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

Fields with 'allow_null=True' should imply a default serialization value#5518

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 3 commits intoencode:masterfromrpkilby:allow-null-default
Oct 30, 2017

Conversation

@rpkilby
Copy link
Contributor

The following currently fails as it's missing adefault value for serialization.

classSerializer(serializers.Serializer):bar=serializers.CharField(source='foo.bar',allow_null=True)

The question is whether or notallow_null=True should implydefault=None.

Thanks to@eschercode for pointing this outhere.

cc@xordoquy

@xordoquy
Copy link
Contributor

@rpkilbyrpkilby changed the titleFields with 'allow_null=True' should imply 'default=None'Fields with 'allow_null=True' should imply a default serialization valueOct 20, 2017
@rpkilbyrpkilby changed the titleFields with 'allow_null=True' should imply a default serialization value[don't merge] Fields with 'allow_null=True' should imply a default serialization valueOct 20, 2017
@shangxiao
Copy link

shangxiao commentedOct 23, 2017
edited
Loading

Aside from discussions about whether flags should imply default values, I'd just would like to point out that if a serializer declares a field then a key for that field should unconditionally exist in the serialized output. The referenced change broke this contract for the first time.

Although: I'd agree if this was rejected due to explicit vs implicit. Perhaps requiring a default to be declared in these cases?

@carltongibson
Copy link
Collaborator

@rpkilby What's the status here? I'm beginning to look at a 3.7.2 (say beginning of next week). Ta!

@rpkilby
Copy link
ContributorAuthor

@carltongibson Basically, trying to figure out what the correct behaviorshould be here.

allow_null=True shouldnot implydefault=None. This was verified by test failures, where you may want to allow null values as input, but require this explicitly during deserialization.

That all said:

Aside from discussions about whether flags should imply default values, I'd just would like to point out that if a serializer declares a field then a key for that field should unconditionally exist in the serialized output. The referenced change broke this contract for the first time.

Is this contract actually true, or is it an assumption made as a result of the previous behavior? Pinging@tomchristie for input here.

@rpkilby
Copy link
ContributorAuthor

At this point, I'm kind of leaning towards accepting the PR. It allows correct serialization of fields when no object relation exists. However, when coupled withrequired=True, a usermust supply a value during deserialization.

@carltongibsoncarltongibson changed the title[don't merge] Fields with 'allow_null=True' should imply a default serialization valueFields with 'allow_null=True' should imply a default serialization valueOct 30, 2017
Copy link
Collaborator

@carltongibsoncarltongibson left a comment

Choose a reason for hiding this comment

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

OK. We're going to take this. Ultimately it seems entailed by the decisions we've already made, and we seemed to agree at each step.

The behaviour now seems consistent. There is (already) a slight change from earlier behaviour; addingdefault=None is recommended.

Maybe there's some weird combination of flags for which the behaviour is unexpected, but we need that pinned down in an actual code example if we are to proceed on it.

Even with such an example the answer might be, "use a custom field in that case" — we're not necessarily committed to covering every possible scenario.

For now, at least, we've spent long enough thinking about this one.

Thanks all.

@rpkilby
Copy link
ContributorAuthor

Just as a followup from#5639..

I'd just would like to point out that if a serializer declares a field then a key for that field should unconditionally exist in the serialized output.
--@RapiLabs

This statement is true,except for non-required fields. Quoting from theField.required docs:

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.

pchiquet pushed a commit to pchiquet/django-rest-framework that referenced this pull requestNov 17, 2020
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@carltongibsoncarltongibsoncarltongibson approved these changes

Assignees

@xordoquyxordoquy

@rpkilbyrpkilby

Labels

Projects

None yet

Milestone

3.7.2 Release

Development

Successfully merging this pull request may close these issues.

4 participants

@rpkilby@xordoquy@shangxiao@carltongibson

[8]ページ先頭

©2009-2025 Movatter.jp