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
Serializer validation error on nullable relations #1303
Comments
Responded against the S/O question... http://stackoverflow.com/questions/20681468/how-to-make-foreignkey-play-nice-with-none-in-drf |
Annddd.... updated. |
@tomchristie Sorry, I've been editing both SO and this issue for the past couple minutes. Didn't realize I would get a response so fast. Both should be complete now (especially here), I think. |
I ran into this today. After some stepping around in a debugger, what's happening is pretty clear. APIRequestFactory's
So later, when the serializer attempts to deserialize the data from the request, the foreign key field contains a literal A naive fix seems like it would be pretty easy (don't encode a string |
I think coercing |
FWIW, this is a bit worse for nullable string fields. For nullable foreign keys, an empty string results in the field getting saved with a null value, which works. For nullable string fields, the empty string gets saved verbatim, and there is no value (that I can see) that the renderer will deserialize as Edit: and of course, it still requires replacing |
@frankpape good points. I think nullable string fields might be something we should ignore for now/table for later discussion. Django would certainly not be the first to deal with this nuisance in terms of JSON (de)serialization. |
@dustinfarris fair enough. I only bring it up because they share the root problem, and a solution to the nullable relations issue will likely impact other field types as well. |
Ok. I'll submit a PR and see how it goes. |
Closed by #1441 thanks to @dustinfarris. |
@tomchristie I'll say yes, but I'm a "release often" kind of guy. Plus, this will benefit several of my own projects so my opinion is biased. :-) |
Little bust this w/e with fam commitments. Ping this ticket again tues or so if I've not already got around to it by then. |
s/bust/busy/ |
Sounds good. |
@tomchristie here is your friendly reminder for a version bump. |
With added sparkles. 💖 ✨ 2.3.13 ✨ 💖 |
Thanks @dustinfarris |
I think this has been danced around before, but I can't remember what the consensus was and Google is not helping me today.
If I have:
Then in shell:
To make this work I have to manually convert the None value to an empty string (which is hinted at in the documentation under the "required" attribute for relation fields):
It seems like the first request should not return a 400 given the model is valid, and the data was created using DRF's own serializer.
In case this is intended behavior, I've also inquired on SO for best practices:
http://stackoverflow.com/questions/20681468/how-to-make-foreignkey-play-nice-with-none-in-drf
The text was updated successfully, but these errors were encountered: