Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Non-required fields with 'allow_null=True' should not imply a default value#5639

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
carltongibson merged 1 commit intoencode:masterfromRomuloOliveira:master
Dec 1, 2017

Conversation

@RomuloOliveira
Copy link
Contributor

Prior to the#5518, all fields withrequired=False and without a explicit default value were not included in the output.

After the patch, even non-required fields now had a default value, which is counter-intuitive if you look at therequired documentation, which saysSet to false if this field is not required to be present during deserialization.

This PR restore the originalrequired=False behaviour. Now, anyrequired=False without a explicit default value should not be returned in the output.
However, it keeps the implicitdefault=None for required fields introduced in#5518.

Question I: Did I wrote the test in the correct class (TestNotRequiredOutput) or should it be in theTestDefaultOutput class?

Question II: Is there a reason that the implicit defaultNone value forallow_null is not in theField.get_default function? Looking at theexcept handling code, at first it did notsound right.

Question III: Should I add to the documentation thatallow_null implicitly set adefault=None for required fields?

That issue is already closed and it is not my intention to reopen it, but it's a special case that I did not fully understand why not forcing a explicit default value. E.g, what aboutallow_blank? Andmany=True? Shouldn't they have a implicit default value too, using the same logic?

@rpkilby
Copy link
Contributor

rpkilby commentedDec 1, 2017
edited
Loading

Hi@RomuloOliveira, thanks for raising this. The docs do say thatrequired fields may be omitted:

Setting this toFalse also allows the object attribute or dictionary key to be omitted from output when serializing the instance. If the key is not present it will simply not be included in the output representation.

To address a few things...

However, it keeps the implicitdefault=None for required fields introduced in#5518.

To clarify, the PR description is not completely correct, and this was not the final behavior.default values affect both serialization and deserialization, andallow_null implyingdefault=None was not the correct behavior (you can see this in the commit/build history for#5518). Instead,allow_null only implies a default value forserialization output.

Did I wrote the test in the correct class (TestNotRequiredOutput) or should it be in theTestDefaultOutput class?

TestNotRequiredOutput is correct, as this isn't testing thedefault argument.

Is there a reason that the implicit defaultNone value forallow_null is not in theField.get_default function?

Yes, this is related toallow_null implyingdefault=None not being the correct behavior.

Should I add to the documentation thatallow_null implicitly set adefault=None for required fields?

I don't think it's necessary for this PR. I'll address it separately.

RomuloOliveira reacted with thumbs up emoji

Copy link
Contributor

@rpkilbyrpkilby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

👍

@carltongibsoncarltongibson added this to thev3.7.4 milestoneDec 1, 2017
@carltongibsoncarltongibson merged commit905a557 intoencode:masterDec 1, 2017
ticosax added a commit to ticosax/django-rest-framework that referenced this pull requestDec 23, 2017
Sinceencode#5639 we are unable to render fields that were rendered previouslywith `allow_null=True`.Backward compatibility is not preserved,because `required=True` and `read_only=True` are conflicting, so in ourcase, some fields just disappeared and it seems there is not way to getthem back.I'm not sure about the fix, but the regression test is probably right.
@rpkilby
Copy link
Contributor

In hindsight, while consistent with therequired docs, I don't think this is the correct behavior. I'm thinking thatallow_null should always imply a default serialization value, regardless of whether a field is required.

@RomuloOliveira, was there a specific reason why these fields needed to be omitted from the serialized output? It's not clear to me what behavior this is enabling.

@RomuloOliveira
Copy link
ContributorAuthor

@rpkilby I've made this to maderequired=False output consistent and simple (not required -> omitted from output, period) and restore this behaviour, which existed before 3.7.2.

In my opinion, it keeps things explicit and do not depend on any other flags combinations to work as expected. In this way, an user do not need to know allcaveats and special behaviour that occurs when flags are combined.

I noticed this because after upgrading to 3.7.2 some fields were not consistent with others in an API that I was developing and it took a lot of time to figure out what was causing it. I've thought at first it was a bug before seeing the discussion#5518.

I thought it's a bit weird thatallow_null=True implies a default serialization output. It is not consistent with python/DRF "philosophy" of keeping things simple and explicit.

This discussion was closed and so was#5518, but as an DRF user, I'm asked myself why use implicit values (which can interfere with another flags) and, why use itonly inallow_null. I think it made inconsistent, sinceallow_blank=True do not imply a blank default output and I really don't think it should.

ticosax added a commit to lock8/django-rest-framework that referenced this pull requestJan 5, 2018
Sinceencode#5639 we are unable to render fields that were rendered previouslywith `allow_null=True`.Backward compatibility is not preserved,because `required=True` and `read_only=True` are conflicting, so in ourcase, some fields just disappeared and it seems there is not way to getthem back.I'm not sure about the fix, but the regression test is probably right.
carltongibson added a commit to carltongibson/django-rest-framework that referenced this pull requestMar 20, 2018
carltongibson added a commit to carltongibson/django-rest-framework that referenced this pull requestMar 20, 2018
carltongibson added a commit that referenced this pull requestMar 20, 2018
* Revert "Non-required fields with 'allow_null=True' should not imply a default value (#5639)"    This reverts commit905a557.Closes#5708* Add test for allow_null + required=False    Ref#5708: allow_null should imply default=None, even for non-required fields.* Re-order allow_null and default in field docs    default is prior to allow_null. allow_null implies an outgoing default=None.* Adjust allow_null note.
pchiquet pushed a commit to pchiquet/django-rest-framework that referenced this pull requestNov 17, 2020
pchiquet pushed a commit to pchiquet/django-rest-framework that referenced this pull requestNov 17, 2020
* Revert "Non-required fields with 'allow_null=True' should not imply a default value (encode#5639)"    This reverts commit905a557.Closesencode#5708* Add test for allow_null + required=False    Refencode#5708: allow_null should imply default=None, even for non-required fields.* Re-order allow_null and default in field docs    default is prior to allow_null. allow_null implies an outgoing default=None.* Adjust allow_null note.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@carltongibsoncarltongibsoncarltongibson approved these changes

+1 more reviewer

@rpkilbyrpkilbyrpkilby approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

3.7.4 Release

Development

Successfully merging this pull request may close these issues.

4 participants

@RomuloOliveira@rpkilby@carltongibson@RomuloSPCO

[8]ページ先頭

©2009-2025 Movatter.jp