Non-required serializer related fields #7748
Replies: 11 comments
-
|
From the documentation:
As such, using |
Beta Was this translation helpful? Give feedback.
-
|
I encountered exactly the same issue, but managed to get it to fail with Here is the whole PR along with the fix #3984 |
Beta Was this translation helpful? Give feedback.
-
|
The issue looks valid to me as stated. |
Beta Was this translation helpful? Give feedback.
-
|
Turns out this isn't valid. |
Beta Was this translation helpful? Give feedback.
-
|
@tomchristie actually it does say so. |
Beta Was this translation helpful? Give feedback.
-
|
True. |
Beta Was this translation helpful? Give feedback.
-
|
I'm going to de-milestone for now. We can reassess after v3.7 |
Beta Was this translation helpful? Give feedback.
-
|
:-O Wow, As a new user, I found it very surprising that I needed to set an entry in data to None to get this working. Especially since the docs say "will not take place if the field is not INCLUDED" (http://www.django-rest-framework.org/api-guide/serializers/#validation)
What would also help is a code sample around
However, the design has a problem: If you are adding fields over time, and you want to maintain some backward compatibility, then it should be safe to add them as require=False without updating all the code to include a new field with a None value. I'm at djangorestframework==3.7.7 |
Beta Was this translation helpful? Give feedback.
-
If you’re removing the relationship that the serializer field points at, then just remove the serializer field too. (Or at least switch it to read_only=True) |
Beta Was this translation helpful? Give feedback.
-
I'm referring to the case of adding a field to a model. If one adds a field to a model, and a adds it to a serializer as I'm thinking of how backwards compatibility is handled in networking code, such as Thrift and Protocall Buffers. But perhaps I'm misunderstanding and my code is just wrong? |
Beta Was this translation helpful? Give feedback.
-
For reference, if other people have trouble with not required values not being omitted from the output, class OmitNoneNotRequiredSerializer(object):
def to_representation(self, instance):
ret = super().to_representation(instance)
for field in self._readable_fields:
if not field.required and ret[field.field_name] is None:
del ret[field.field_name]
return retJust add this as a mixin (first class argument) to your serializers to omit every class FooSerializer(OmitNoneNotRequiredSerializer,
serializers.HyperlinkedModelSerializer):
...But this raises the issue how the behaviour should be if |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checklist
masterbranch of Django REST framework.Steps to reproduce
create a serializer that subclasses
serializers.Serializerwith a non-required primary key relationcall the serializer in with a request that doesn't have a user parameter
Expected behavior
the serializer should validate that the field
qexists and ignore validating theuserfield if it is not supplied in the request data. The an example request{ "q": "hello world" }should be valid, and user should either be
Noneor not included in theserializer.dataActual behavior
the serializer raises a
KeyErroronserializer.dataif user is not included in the request data.Beta Was this translation helpful? Give feedback.
All reactions