-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Non-required serializer related fields #3976
Comments
From the documentation:
As such, using |
I encountered exactly the same issue, but managed to get it to fail with Here is the whole PR along with the fix #3984 |
The issue looks valid to me as stated. |
Turns out this isn't valid. |
@tomchristie actually it does say so. |
True. |
I'm going to de-milestone for now. We can reassess after v3.7 |
:-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 |
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) |
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? |
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 ret Just 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 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Checklist
master
branch of Django REST framework.Steps to reproduce
create a serializer that subclasses
serializers.Serializer
with 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
q
exists and ignore validating theuser
field if it is not supplied in the request data. The an example requestshould be valid, and user should either be
None
or not included in theserializer.data
Actual behavior
the serializer raises a
KeyError
onserializer.data
if user is not included in the request data.The text was updated successfully, but these errors were encountered: