-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
One field that is required=False, allow_null=True
and not provided. to_representation should not explicitly set it to None
#9458
Comments
required=False, allow_null=True
and not provided. to_representation should not explicitly set it to None
My Python version is 3.7, so I am using djangorestframework 3.15.1. However, the issue still persists |
Currently the Thus the use-case you are willing is not that for which that parameter was intended. The change in #9461 may cause breaking changes in applications which use DRF. |
Sorry, maybe I didn't describe it clearly. This issue is not related to the Perhaps my pull request has issues, but I hope this problem can be resolved. |
As a user of DRF, |
Currently I belive that this can be work-arounded by providing a callable default which raises a def skip_default():
raise serializers.SkipField
class MySerializer(serializers.Serializer):
name = serializers.CharField()
type = serializers.CharField(required=False, allow_null=True, default=skip_default)
desc = serializers.CharField(required=False, allow_null=True, default=skip_default) A possible solution to this could be to have a different usage of the An other possible solution would be to add a specific parameter to control the output when |
Why not directly modify the class Field:
def __init__(self, read_only=False, write_only=False,
required=None, default=empty, initial=empty, source=None,
label=None, help_text=None, style=None,
error_messages=None, validators=None, allow_null=False):
...
if required is False and allow_null is True and default is empty:
self.default = skip_default |
salom Qandaysiz |
Problem Statement
Currently, one field that is
required=False, allow_null=True
and not provided.to_representation
should not explicitly set it to None.Maybe
rest_framework/fields.py
Example
Consider the following serializer and instance:
The text was updated successfully, but these errors were encountered: