-
-
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
Fix None UUID ForeignKey serialization #3915
Conversation
Well the build is failing for Django 1.7 with a weird one:
A quick Google suggests restarting Apache (!?) — It's not one I've seen before. Any ideas? Django 1.7? :-) |
Oh... of course... UUIDField isn't available in Django 1.7. (I'll look at that tomorrow) Django 1.7? :-) |
Since Django 1.7 is EOL I dropped it from the build. Happy to re-add and work around it if needed, but IMO it's a good thing to now support. Let me know. |
Well, we'll also need to update the release notes for 3.4 if we drop 1.7 for it (I'm fine with this). |
@xordoquy I updated the release notes. |
1fe563f
to
fe18e35
Compare
fe18e35
to
14e52ca
Compare
OK. I worked it out: the key is |
@carltongibson Would it make sense to target this fix at 3.3.3, and have the Django deprecation as a separate PR targeting 3.4.0? |
Hey @tomchristie — Only issue there is the failure.
It's because we end up importing UUIDField into the test suite. Is it worth branching on that? You tell me — I don't mind. — What's the easiest way to avoid the issue for now? |
This reverts commit 50782ec.
OK... Let's see how this one goes... |
OK. Same error. My version check obviously isn't right. Advice please. 😃 |
As this stands I'd be inclined to reset to carltongibson@14e52ca If you can point me to an easy way to save Django 1.7 compat for now I can add that, if it's worth it. (Version numbers are cheap and anyone still on 1.7 has more important things to worry about than upgrading DRF...) |
@@ -778,6 +778,8 @@ def to_internal_value(self, data): | |||
return data | |||
|
|||
def to_representation(self, value): | |||
if value is None: | |||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The None
case is handled as part of the regular serialization machinery - shouldn't need to be handling them on a per-field basis. (Tho perhaps something else wonky in this case?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the first commit here: carltongibson@ab3f9cf
(Before getting distracted with 1.7)
The test fails without this change. If this is the wrong fix, then where should it go? (I'm happy to dig but you probably Just Know™)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here PrimaryKeyRelatedField
calls self.pk_field.to_representation(value.pk)
.
When that's a UUIDField
we hit this block:
if self.uuid_format == 'hex_verbose':
return str(value)
When value
is None
, without the fix, that's equivalent to:
>>> str(None)
'None'
which is the observed output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Good link. I see it.
I'll have a look later.
(Do you have any thoughts on keeping/dropping Django 1.7 here? Happy to keep it but not sure how, since the if ...VERSION
fixes didn't work...?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with the 3.4 line dropping the 1.7.
I would feel better if that was part of a different PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how to make the tests pass... — I could do the 1.7 one first...
Replaced by #3936 |
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.