-
-
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
Better catching of error cases in .update
and .create
.
#2202
Comments
Possible that we should make this case raise an explicit error explaining why it can't be dealt with automatically. You need to write an explicit create and/or update method here. Eg. There isnt any way of automatically handling this case: What are you expecting to happen on create? Create a new user instance? If so how would you determine a username? You probably only want to support updates on this serializer and override create to raise 'NotImplemented' but if that's not the case then you'll need to figure out what it is you want to happen instead. I've no doubt that 2.x would present some horrible buggy behaviour here. |
Incidentally what type was the exception you got? Would be useful to know so we can improve the error messaging. |
Pretty sure this is a |
We could expand on the exception handling then |
Yep, ValueError.
|
How about the following?...
|
Even more comprehensive would be to check for dotted source notation in the same way we do for writable nested fields. |
Here's my perspective as a newbie (and assuming My first response to that was similar to my response to the error I got: "Guess I can't do that, then." As I thought about it while finishing breakfast, my response shifted towards: "But if I really wanted to do it maybe that would tell me how if I were more familiar with the framework." At the moment, though, I've settled on, "Why is it trying to create something when I'm trying to update an existing record? I must really not be doing this the right way." If those are reasonable things to think in this situation, then that message would have the correct effect. |
Oops my error above is incorrect of course - this is not during As it happens for "to one" relationships we could try to handle this automatically for updates only but there would still be error cases to deal with there (eg nullable relationship that does not exist) so not sure if we should try to do it automagically, or if we should force the code to be explicit. |
.update
and .create
.
I noticed this is closed but I'm still getting the exact same error response. It's also a little annoying that I can't even remove the attribute during validation or creation and then run super().create. |
Override |
Yeah, I looked at things a bit closer and realized it was storing the attribute in I'm still curious as to why I wasn't getting the new exception though. |
When I do a PUT through the API browser to a view whose serializer contains a source property:
I receive the following error:
I've created a bare-bones django project that reproduces the error. Of particular note are the serializers and the model.
Relevant excerpt of IRC conversation leading to this ticket:
The text was updated successfully, but these errors were encountered: