Working through Django Rest Framework Tutorial - Serialization
In SnippetSerializer(serializers.Serializer):
title = serializers.CharField(required=False, max_length=100)
Gives an error: ('title': ['This field may not be blank.'])
Model:
# Added null=True to see if that had any effect which it did not.
title = models.CharField(max_length=100, blank=True, null=True, default='')
Python 3.4.2 (default, Nov 20 2014, 15:46:21)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from snippets.models import Snippet
>>> from snippets.serializers import SnippetSerializer
>>> from rest_framework.renderers import JSONRenderer
>>> from rest_framework.parsers import JSONParser
>>> snippet = Snippet(code='foo = "bar"\n')
>>> snippet.save()
>>> snippet = Snippet(code='print "hello, world"\n')
>>> snippet.save()
>>> serializer = SnippetSerializer(snippet)
>>> serializer.data
ReturnDict([('pk', 5), ('title', ''), ('code', 'print "hello, world"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')])
>>> content = JSONRenderer().render(serializer.data)
>>> content
b'{"pk":5,"title":"","code":"print \\"hello, world\\"\\n","linenos":false,"language":"python","style":"friendly"}'
>>> from rest_framework.compat import BytesIO
>>> stream = BytesIO(content)
>>> data = JSONParser().parse(stream)
>>> serializer = SnippetSerializer(data=data)
>>> serializer.is_valid()
False
>>> serializer.errors
ReturnDict([('title', ['This field may not be blank.'])])
>>>
My guess is that because we don't have allow_blank=True set for the CharField, it's triggering the error (as documented).
Should we add allow_blank=True to the documentation, or is there a deeper issue?
Adding allow_blank=True:
title = serializers.CharField(required=False, max_length=100, allow_blank=True)
Solves the problem with the error message like you pointed out. Definitely should be added to the tutorial documentation at the very least.
The tutorial is miss leading in Creating a Serializer class it shows only using required=False, however in Using ModelSerializers shows allow_blank=True is automatically used.
>>> print repr(serializer) # In python 3 use `print(repr(serializer))`
SnippetSerializer():
id = IntegerField(label='ID', read_only=True)
title = CharField(allow_blank=True, max_length=100, required=False)
Yes, adding allow_blank=True is the right thing here.
Now resolved... http://www.django-rest-framework.org/tutorial/1-serialization/#creating-a-serializer-class
if the dev web interface to the API allowed you to not pass through required=False fields then this would probably be more obvious to people trying it out there—but that’d probably be a bit of a clumsy interface
actually — @tomchristie does format="multipart" force all fields to be serialized in a response, while format="json" does not? (Could that be part of why there’s no option to exclude a field?) I’ve encountered a discrepancy in my code with a required=False nested serializer where…
format="json" it just excludes the entire nested value in its responseIf there’s not a quick/clear answer, I can move these questions to SO instead
Upon review, it seems like a different enough problem, I debugged it a bunch and opened a separate issue https://github.com/tomchristie/django-rest-framework/issues/2779
Most helpful comment
Adding allow_blank=True:
Solves the problem with the error message like you pointed out. Definitely should be added to the tutorial documentation at the very least.
The tutorial is miss leading in Creating a Serializer class it shows only using required=False, however in Using ModelSerializers shows allow_blank=True is automatically used.