Handle unset fields with 'many=True'#7574
Merged
lovelydinosaur merged 2 commits intoencode:masterfrom Jun 8, 2022
Merged
Conversation
The docs note: When serializing fields with dotted notation, it may be necessary to provide a `default` value if any object is not present or is empty during attribute traversal. However, this doesn't work for fields with 'many=True'. When using these, the default is simply ignored. The solution is simple: do in 'ManyRelatedField' what we were already doing for 'Field', namely, catch possible 'AttributeError' and 'KeyError' exceptions and return the default if there is one set. Signed-off-by: Stephen Finucane <[email protected]> Closes: encode#7550
Signed-off-by: Stephen Finucane <[email protected]>
Contributor
Author
|
Bump. Is there anything else I need to do to get eyes on this? Happy to propose backports to stable branches after the fact too. |
stephenfin
added a commit
to getpatchwork/patchwork
that referenced
this pull request
Dec 13, 2020
This wasn't writeable for reasons I haven't been able to figure out. However, it's not actually needed: the 'PatchSerializer' can do the job just fine, given enough information. This exposes a bug in DRF, which has been reported upstream [1]. While we wait for that fix, or some variant of it, to be merged, we must monkey patch the library. [1] encode/django-rest-framework#7550 [2] encode/django-rest-framework#7574 Signed-off-by: Stephen Finucane <[email protected]> Reported-by: Ralf Ramsauer <[email protected]> Closes: #379 Cc: Daniel Axtens <[email protected]> Cc: Rohit Sarkar <[email protected]>
stephenfin
added a commit
to getpatchwork/patchwork
that referenced
this pull request
Dec 13, 2020
This wasn't writeable for reasons I haven't been able to figure out. However, it's not actually needed: the 'PatchSerializer' can do the job just fine, given enough information. This exposes a bug in DRF, which has been reported upstream [1]. While we wait for that fix, or some variant of it, to be merged, we must monkey patch the library. Conflicts: patchwork/api/patch.py NOTE(stephenfin): Conflicts are due to the absence of commit d3d4f9f ("Add Django 3.0 support") which we do not want to backport here. [1] encode/django-rest-framework#7550 [2] encode/django-rest-framework#7574 Signed-off-by: Stephen Finucane <[email protected]> Reported-by: Ralf Ramsauer <[email protected]> Closes: #379 Cc: Daniel Axtens <[email protected]> Cc: Rohit Sarkar <[email protected]> (cherry picked from commit fe07f30)
|
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. |
Contributor
Author
|
Bump |
Contributor
|
Verified to myself that this brings the behaviour into line with what we do in We'd normally be risk-averse to a moderately impactful change like this, but because it only changes behaviour in the case of exceptions, this seems like a clear improvement/bug fix to me. So, yes, let's go with it. Thanks & appreciate your patience. |
sigvef
pushed a commit
to sigvef/django-rest-framework
that referenced
this pull request
Dec 3, 2022
* Handle unset fields with 'many=True' The docs note: When serializing fields with dotted notation, it may be necessary to provide a `default` value if any object is not present or is empty during attribute traversal. However, this doesn't work for fields with 'many=True'. When using these, the default is simply ignored. The solution is simple: do in 'ManyRelatedField' what we were already doing for 'Field', namely, catch possible 'AttributeError' and 'KeyError' exceptions and return the default if there is one set. Signed-off-by: Stephen Finucane <[email protected]> Closes: encode#7550 * Add test cases for encode#7550 Signed-off-by: Stephen Finucane <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The docs note:
However, this doesn't work for fields with
many=True. When using these, the default is simply ignored.The solution is simple: do in
ManyRelatedFieldwhat we were already doing forField, namely, catch possibleAttributeErrorandKeyErrorexceptions and return the default if there is one set.I still need to add tests. Input here would be appreciated 😄
Refs: #7550