You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
One field that is required=False, allow_null=True and not provided. to_representation should not explicitly set it to None #9458
defget_attribute(self, instance""" Given the *outgoing* object instance, return the primitive value that should be used for this field. """
...
# changedifnotself.required:
raiseSkipField()
ifself.allow_null:
returnNone**
...
changed the title [-]Include required=False Fields with None Value in to_representation with partial=True[/-][+]One field that is `required=False, allow_null=True` and not provided. to_representation should not explicitly set it to None[/+]on Jul 5, 2024
Currently the partial keyword argument provided to serializers is meant for "partial update", not for "partial representation".
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.
Reacted by Asif Saif Uddin {"Auvi":"অভি"}, Nikita Borodenko and Aleksei Iakovlev
Currently the partial keyword argument provided to serializers is meant for "partial update", not for "partial representation".
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 partial parameter, but rather to the configuration when both required=False and allow_null=True are set together. As described in the example, I'm getting unexpected output.
Perhaps my pull request has issues, but I hope this problem can be resolved.
As a user of DRF, required=False and allow_null=True indicate that this field is optional and can be null. However, this does not mean that the field's default value is None. The changes in refs #5888 have made the meaning of allow_null=True unclear, as it now encompasses two implications: 1) it allows null values, and 2) it defaults to None.
A possible solution to this could be to have a different usage of the allow_null using a more complex value check (ie: True/False -> current behaviour, None -> skip), which is a breaking change (since currently any falsy value produces the same behaviour.
An other possible solution would be to add a specific parameter to control the output when required and allow_null parameters fall into this case.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Activity
[-]Include required=False Fields with None Value in to_representation with partial=True[/-][+]One field that is `required=False, allow_null=True` and not provided. to_representation should not explicitly set it to None[/+]LinanV commentedon Jul 8, 2024
My Python version is 3.7, so I am using djangorestframework 3.15.1. However, the issue still persists
sevdog commentedon Jul 8, 2024
Currently the
partialkeyword argument provided to serializers is meant for "partial update", not for "partial representation".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.
LinanV commentedon Jul 8, 2024
Sorry, maybe I didn't describe it clearly. This issue is not related to the
partialparameter, but rather to the configuration when bothrequired=Falseandallow_null=Trueare set together. As described in the example, I'm getting unexpected output.Perhaps my pull request has issues, but I hope this problem can be resolved.
sevdog commentedon Jul 8, 2024
I suggest to look at #5888, because there there is a change which is the opposite of #9461 (the same change was made in #5639).
LinanV commentedon Jul 9, 2024
As a user of DRF,
required=Falseandallow_null=Trueindicate that this field is optional and can benull. However, this does not mean that the field's default value isNone. The changes in refs #5888 have made the meaning ofallow_null=Trueunclear, as it now encompasses two implications: 1) it allows null values, and 2) it defaults toNone.sevdog commentedon Jul 9, 2024
Currently I belive that this can be work-arounded by providing a callable default which raises a
SkipFieldexception.A possible solution to this could be to have a different usage of the
allow_nullusing a more complex value check (ie:True/False-> current behaviour,None-> skip), which is a breaking change (since currently any falsy value produces the same behaviour.An other possible solution would be to add a specific parameter to control the output when
requiredandallow_nullparameters fall into this case.LinanV commentedon Jul 9, 2024
Why not directly modify the
Fieldclass, instead of requiring users to explicitly define thedefaultparameter?Rustambek0401 commentedon Jul 23, 2024
salom Qandaysiz
stale commentedon Oct 18, 2025
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.