Commit 65f59286 by Tom Christie

Fix issue where required fields were not being properly validated. Also make…

Fix issue where required fields were not being properly validated.  Also make model fields with a default value be not required
parent 4a21b355
......@@ -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
......
......@@ -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 == '*':
......
......@@ -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
......
......@@ -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):
"""
......
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):
......
......@@ -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):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment