8👍
I’m not sure what’s going on there, we’ve got coverage for that case and similar cases work fine for me.
Perhaps try dropping into the shell and inspecting the serializer directly.
For example if you instantiate the serializer, what does serializer.fields
return? How about serializer.field['type'].null
? If you pass data to the serializer directly in the shell what results do you get?
For example:
serializer = ProductSerializer(data={'barcode': 'foo', 'type': None})
print serializer.is_valid()
print serializer.errors
If you get some answers to those, update the question and we’ll see if we can get it sorted.
Edit
Okay, that explains things better. The ‘type’ field is nullable, so it may be None
, but it’s still a required field. If you want it to be null you have to explicitly set it to None
.
If you really do want to be able to exclude the field when POSTing the data, you can include the required=False
flag on the serializer field.
17👍
Add the kwarg allow_null
when initializing the serializer:
class ProductSerializer(serializers.ModelSerializer):
type = serializers.PrimaryKeyRelatedField(null=True, source='type', allow_null=True)
As already mentioned in the comment of @gabn88 but I think it warrants its own answer. (Cost me some time because I only read that comment after finding it out by myself.)
See http://www.django-rest-framework.org/api-guide/relations/
- [Django]-How TDD can be applied to Django Class based Generic Views?
- [Django]-Django static page?
- [Django]-OSQA vs. Askbot?