-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Handle unset fields with 'many=True' #7574
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]>
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. |
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]>
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. |
Bump |
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. |
* 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]>
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
ManyRelatedField
what we were already doing forField
, namely, catch possibleAttributeError
andKeyError
exceptions and return the default if there is one set.I still need to add tests. Input here would be appreciated 😄
Refs: #7550