Skip to content
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

Merged
merged 2 commits into from
Jun 8, 2022

Conversation

stephenfin
Copy link
Contributor

@stephenfin stephenfin commented 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.

I still need to add tests. Input here would be appreciated 😄

Refs: #7550

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]>
@stephenfin
Copy link
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)
@stale
Copy link

stale bot commented Apr 30, 2022

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.

@stale stale bot added the stale label Apr 30, 2022
@stephenfin
Copy link
Contributor Author

Bump

@stale stale bot removed the stale label May 2, 2022
@tomchristie
Copy link
Member

Verified to myself that this brings the behaviour into line with what we do in Field, yup.

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.

@tomchristie tomchristie merged commit 5185cc9 into encode:master Jun 8, 2022
@stephenfin stephenfin deleted the issue/7550 branch June 8, 2022 14:59
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants