toastdriven/django-tastypie — Issue #369: POST data isn't validated based on Model if it doesn't exist in request
Author: aljosa
Created: 2012-02-10 16:47:55+00:00
State: open

Node model requires name but if "name" doesn't exist in request POST data tastypie validation passes and a new Node entry is created.
created entry can't be saved in admin app because validation fails on "name" which is empty.
have no idea how tastypie managed to create this entry, hope i'm wrong about this.

# models.py
class Node(models.Model):
name = models.CharField(max_length=255)

# api.py
class NodeResource(ModelResource):
class Meta:
queryset = Node.objects.all()
resource_name = "nodes"
allowed_methods = ['get', 'post',]
fields = ['id', 'name',]
authorization = DevAuthorization()
authentication = DevAuthentication()
validation = Validation() # same result w/ or w/o
always_return_data = True

# POST request

import requests, json

base_url = "http://localhost:8000"
headers = {'content-type': 'application/json'}

new_node = requests.post("%s%s" % (base_url, "/api/v1/nodes/"), data=json.dumps({}), headers=headers)
print "status_code: ", new_node.status_code

# output
$ status_code: 201