Fix issue where required fields were not being properly validated. Also make model fields with a default value be not required

This commit is contained in:
Tom Christie 2012-10-08 17:53:18 +01:00
parent 4a21b3557e
commit 65f592866c
6 changed files with 25 additions and 6 deletions

View File

@ -32,7 +32,7 @@ REST framework also includes [serialization] and [parser]/[renderer] components
### What REST framework *doesn't* provide.
What REST framework doesn't do is give you is machine readable hypermedia formats such as [Collection+JSON][collection] by default, or the ability to auto-magically create HATEOAS style APIs. Doing so would involve making opinionated choices about API design that should really remain outside of the framework's scope.
What REST framework doesn't do is give you is machine readable hypermedia formats such as [Collection+JSON][collection] or HTML [microformats] by default, or the ability to auto-magically create HATEOAS style APIs. Doing so would involve making opinionated choices about API design that should really remain outside of the framework's scope.
[cite]: http://vimeo.com/channels/restfest/page:2
[dissertation]: http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
@ -45,6 +45,7 @@ What REST framework doesn't do is give you is machine readable hypermedia format
[maturitymodel]: http://martinfowler.com/articles/richardsonMaturityModel.html
[collection]: http://www.amundsen.com/media-types/collection/
[microformats]: http://microformats.org/wiki/Main_Page
[serialization]: ../api-guide/serializers.md
[parser]: ../api-guide/parsers.md
[renderer]: ../api-guide/renderers.md

View File

@ -161,7 +161,9 @@ class WritableField(Field):
try:
native = data[field_name]
except KeyError:
return # TODO Consider validation behaviour, 'required' opt etc...
if self.required:
raise ValidationError(self.error_messages['required'])
return
value = self.from_native(native)
if self.source == '*':

View File

@ -225,7 +225,6 @@ class Request(object):
if (self._METHOD_PARAM and
self._METHOD_PARAM in self._data):
self._method = self._data[self._METHOD_PARAM].upper()
self._data.pop(self._METHOD_PARAM)
# Content overloading - modify the content type, and force re-parse.
if (self._CONTENT_PARAM and

View File

@ -371,9 +371,14 @@ class ModelSerializer(Serializer):
models.BooleanField: BooleanField,
}
try:
return field_mapping[model_field.__class__]()
ret = field_mapping[model_field.__class__]()
except KeyError:
return ModelField(model_field=model_field)
ret = ModelField(model_field=model_field)
if model_field.default:
ret.required = False
return ret
def restore_object(self, attrs, instance=None):
"""

View File

@ -1,3 +1,4 @@
import copy
import datetime
from django.test import TestCase
from rest_framework import serializers
@ -93,6 +94,15 @@ class ValidationTests(TestCase):
self.assertEquals(serializer.is_valid(), False)
self.assertEquals(serializer.errors, {'content': [u'Ensure this value has at most 1000 characters (it has 1001).']})
def test_update_missing_field(self):
data = {
'content': 'xxx',
'created': datetime.datetime(2012, 1, 1)
}
serializer = CommentSerializer(data, instance=self.comment)
self.assertEquals(serializer.is_valid(), False)
self.assertEquals(serializer.errors, {'email': [u'This field is required.']})
class MetadataTests(TestCase):
def test_empty(self):

View File

@ -17,12 +17,14 @@ class BasicView(APIView):
return Response({'method': 'POST', 'data': request.DATA})
@api_view(['GET', 'POST'])
@api_view(['GET', 'POST', 'PUT'])
def basic_view(request):
if request.method == 'GET':
return {'method': 'GET'}
elif request.method == 'POST':
return {'method': 'POST', 'data': request.DATA}
elif request.method == 'PUT':
return {'method': 'PUT', 'data': request.DATA}
class ClassBasedViewIntegrationTests(TestCase):