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

Fix None UUID ForeignKey serialization#3936

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
xordoquy merged 3 commits intoencode:masterfromcarltongibson:null-uuid-fk-take2
Mar 22, 2016

Conversation

@carltongibson
Copy link
Collaborator

Replaces#3915

A nullable foreign key with a UUID primary key on the other end would result in incorrect serialisation as (the string) 'None'.

This PR adds a minimal test case and fix.

@carltongibson
Copy link
CollaboratorAuthor

As per@tomchristie'scomment this might not be the right fix.

Related:https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/serializers.py#L461-L472

and

Question is why that's not catching the None case. Are we always failing to do so for related fields, or is there something different in this case?

@carltongibson
Copy link
CollaboratorAuthor

Right. Progress.

Slapping a breakpoint in where@tomchristie said reveals the problem:

rest_framework/serializers.py(469)to_representation()-> if attribute is None:(Pdb) fieldPrimaryKeyRelatedField(allow_null=True, pk_field=UUIDField(), queryset=[])(Pdb) attribute<rest_framework.relations.PKOnlyObject object at 0x1122de7d0>(Pdb) l464                 except SkipField:465                     continue466     467                 import pdb; pdb.set_trace()468     469  ->             if attribute is None:470                     # We skip `to_representation` for `None` values so that471                     # fields do not have to explicitly deal with that case.472                     ret[field.field_name] = None473                 else:474                     ret[field.field_name] = field.to_representation(attribute)(Pdb)

In this case at leastattribute isnotNone, even though the relation inNone. Instead we have aPKOnlyObject object.

@carltongibson
Copy link
CollaboratorAuthor

Where a foreign key is nullRelatedField.get_attribute will returnNone isall cases except whereuse_pk_only_optimization returnsTrue. Only then will the existingattribute is None check give the wrong result.

Thus in2ef74cf I resolve thepk value before checking forNone. This allows thead hoc code added toUUIDField to be removed, and the added tests still pass. 🎉

I'm happy to adjust the formatting of theA if B else C if you don't like that.

Otherwise I think this is probably the right fix. Thoughts?

@carltongibsoncarltongibson added this to the3.4.0 Release milestoneMar 22, 2016
@carltongibson
Copy link
CollaboratorAuthor

Just milestoning this to keep it on the radar.

@xordoquy
Copy link
Contributor

@carltongibson does this need a review ?

@carltongibson
Copy link
CollaboratorAuthor

I think it's the right fix — but I'd be happy if you or@tomchristie could just think it through... — is there another case we've missed? (Or shall we handle that if/when it comes up)

@xordoquy
Copy link
Contributor

It looks good to me. I don't see other cases.
Let's move on.

On a side note, I'd be interested in discussing the design (ping@tomchristie) esp. why this has to be in the serializer as opposed as@carltongibson's initial proposal - which was to handle that within the field.

xordoquy added a commit that referenced this pull requestMar 22, 2016
@xordoquyxordoquy merged commit0e83063 intoencode:masterMar 22, 2016
@xordoquy
Copy link
Contributor

Nice job 👍

lovelydinosaur reacted with thumbs up emojixordoquy and kevin-brown reacted with hooray emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

3.4.0 Release

Development

Successfully merging this pull request may close these issues.

2 participants

@carltongibson@xordoquy

[8]ページ先頭

©2009-2025 Movatter.jp