From 65f592866c5cd5103e99ed453543807bcbdaa9da Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 8 Oct 2012 17:53:18 +0100 Subject: [PATCH] Fix issue where required fields were not being properly validated. Also make model fields with a default value be not required --- docs/topics/rest-hypermedia-hateoas.md | 3 ++- rest_framework/fields.py | 4 +++- rest_framework/request.py | 1 - rest_framework/serializers.py | 9 +++++++-- rest_framework/tests/serializer.py | 10 ++++++++++ rest_framework/tests/views.py | 4 +++- 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/docs/topics/rest-hypermedia-hateoas.md b/docs/topics/rest-hypermedia-hateoas.md index 3aa1ff5f3..0e183a492 100644 --- a/docs/topics/rest-hypermedia-hateoas.md +++ b/docs/topics/rest-hypermedia-hateoas.md @@ -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 diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 336eac1e4..33f25a006 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -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 == '*': diff --git a/rest_framework/request.py b/rest_framework/request.py index 3ce931817..3725b3c9c 100644 --- a/rest_framework/request.py +++ b/rest_framework/request.py @@ -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 diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 1770c4ce9..2141619f6 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -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): """ diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 08a7a9d07..4454791b2 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -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): diff --git a/rest_framework/tests/views.py b/rest_framework/tests/views.py index 61b288c68..3746d7c8c 100644 --- a/rest_framework/tests/views.py +++ b/rest_framework/tests/views.py @@ -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):