-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Combination of many=True
and a dotted source doesn't allow a default
#7550
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
Comments
stephenfin
added a commit
to stephenfin/django-rest-framework
that referenced
this issue
Oct 3, 2020
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 <stephen@that.guru> Closes: encode#7550
stephenfin
added a commit
to stephenfin/django-rest-framework
that referenced
this issue
Oct 3, 2020
Signed-off-by: Stephen Finucane <stephen@that.guru>
stephenfin
added a commit
to stephenfin/django-rest-framework
that referenced
this issue
Oct 3, 2020
Signed-off-by: Stephen Finucane <stephen@that.guru>
stephenfin
added a commit
to getpatchwork/patchwork
that referenced
this issue
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 <stephen@that.guru> Reported-by: Ralf Ramsauer <ralf.ramsauer@oth-regensburg.de> Closes: #379 Cc: Daniel Axtens <dja@axtens.net> Cc: Rohit Sarkar <rohitsarkar5398@gmail.com>
stephenfin
added a commit
to getpatchwork/patchwork
that referenced
this issue
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 <stephen@that.guru> Reported-by: Ralf Ramsauer <ralf.ramsauer@oth-regensburg.de> Closes: #379 Cc: Daniel Axtens <dja@axtens.net> Cc: Rohit Sarkar <rohitsarkar5398@gmail.com> (cherry picked from commit fe07f30)
tomchristie
pushed a commit
that referenced
this issue
Jun 8, 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 <stephen@that.guru> Closes: #7550 * Add test cases for #7550 Signed-off-by: Stephen Finucane <stephen@that.guru>
sigvef
pushed a commit
to sigvef/django-rest-framework
that referenced
this issue
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 <stephen@that.guru> Closes: encode#7550 * Add test cases for encode#7550 Signed-off-by: Stephen Finucane <stephen@that.guru>
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Uh oh!
There was an error while loading. Please reload this page.
Checklist
master
branch of Django REST framework.Steps to reproduce
Consider a model with a many-many relationship to itself via a through model.
We can attempt to serialize this using something like the following.
(NOTE: I haven't tested this yet. My case is a lot more complicated so I'll try reduce that is this reproducer isn't valid)
Expected behavior
This is intended to flatten the default output from:
to
Actual behavior
We get an exception:
As discussed in #5489, you need to have a default value if you wish to use dotted notation with a nullable value.
This fixes things for empty case. However, we also need to specify
many=True
to correct the not-empty case otherwise we get the following error:If we do that, we now get:
for the empty case 😞
The text was updated successfully, but these errors were encountered: