mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-18 04:20:53 +03:00
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:
parent
4a21b3557e
commit
65f592866c
|
@ -32,7 +32,7 @@ REST framework also includes [serialization] and [parser]/[renderer] components
|
||||||
|
|
||||||
### What REST framework *doesn't* provide.
|
### 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
|
[cite]: http://vimeo.com/channels/restfest/page:2
|
||||||
[dissertation]: http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
|
[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
|
[maturitymodel]: http://martinfowler.com/articles/richardsonMaturityModel.html
|
||||||
|
|
||||||
[collection]: http://www.amundsen.com/media-types/collection/
|
[collection]: http://www.amundsen.com/media-types/collection/
|
||||||
|
[microformats]: http://microformats.org/wiki/Main_Page
|
||||||
[serialization]: ../api-guide/serializers.md
|
[serialization]: ../api-guide/serializers.md
|
||||||
[parser]: ../api-guide/parsers.md
|
[parser]: ../api-guide/parsers.md
|
||||||
[renderer]: ../api-guide/renderers.md
|
[renderer]: ../api-guide/renderers.md
|
||||||
|
|
|
@ -161,7 +161,9 @@ class WritableField(Field):
|
||||||
try:
|
try:
|
||||||
native = data[field_name]
|
native = data[field_name]
|
||||||
except KeyError:
|
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)
|
value = self.from_native(native)
|
||||||
if self.source == '*':
|
if self.source == '*':
|
||||||
|
|
|
@ -225,7 +225,6 @@ class Request(object):
|
||||||
if (self._METHOD_PARAM and
|
if (self._METHOD_PARAM and
|
||||||
self._METHOD_PARAM in self._data):
|
self._METHOD_PARAM in self._data):
|
||||||
self._method = self._data[self._METHOD_PARAM].upper()
|
self._method = self._data[self._METHOD_PARAM].upper()
|
||||||
self._data.pop(self._METHOD_PARAM)
|
|
||||||
|
|
||||||
# Content overloading - modify the content type, and force re-parse.
|
# Content overloading - modify the content type, and force re-parse.
|
||||||
if (self._CONTENT_PARAM and
|
if (self._CONTENT_PARAM and
|
||||||
|
|
|
@ -371,9 +371,14 @@ class ModelSerializer(Serializer):
|
||||||
models.BooleanField: BooleanField,
|
models.BooleanField: BooleanField,
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
return field_mapping[model_field.__class__]()
|
ret = field_mapping[model_field.__class__]()
|
||||||
except KeyError:
|
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):
|
def restore_object(self, attrs, instance=None):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import copy
|
||||||
import datetime
|
import datetime
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
@ -93,6 +94,15 @@ class ValidationTests(TestCase):
|
||||||
self.assertEquals(serializer.is_valid(), False)
|
self.assertEquals(serializer.is_valid(), False)
|
||||||
self.assertEquals(serializer.errors, {'content': [u'Ensure this value has at most 1000 characters (it has 1001).']})
|
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):
|
class MetadataTests(TestCase):
|
||||||
def test_empty(self):
|
def test_empty(self):
|
||||||
|
|
|
@ -17,12 +17,14 @@ class BasicView(APIView):
|
||||||
return Response({'method': 'POST', 'data': request.DATA})
|
return Response({'method': 'POST', 'data': request.DATA})
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET', 'POST'])
|
@api_view(['GET', 'POST', 'PUT'])
|
||||||
def basic_view(request):
|
def basic_view(request):
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
return {'method': 'GET'}
|
return {'method': 'GET'}
|
||||||
elif request.method == 'POST':
|
elif request.method == 'POST':
|
||||||
return {'method': 'POST', 'data': request.DATA}
|
return {'method': 'POST', 'data': request.DATA}
|
||||||
|
elif request.method == 'PUT':
|
||||||
|
return {'method': 'PUT', 'data': request.DATA}
|
||||||
|
|
||||||
|
|
||||||
class ClassBasedViewIntegrationTests(TestCase):
|
class ClassBasedViewIntegrationTests(TestCase):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user