-
-
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 UniqueTogetherValidator with field sources #7086
Conversation
Hey @rpkilby. Thanks for this. Tricky stuff. 🌶 Let me give it a look over. (But first glance it seems clean.) |
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.
TBH, this looks great. I was worried it was going to be much dirtier than it is. That source
just is the field name in the usual case is nice.
As to potential breaking changes, we may want to call out that validation against read-only fields that have defaults now obey their sources? ¯_(ツ)_/¯
Meh, this is clearly a bug. You can't be depending on the broken behaviour here. 😝
That the existing tests here pass is 👍 for me — We did exercise the difficult cases here (when we changed the old behaviour in 3.8(?)
The initial implementation definitely was. At first, I didn't fully understand that the |
Expanded the writable field test to include validation for a missing value. This shouldn't extend to read-only fields, since their defaults are always provided. |
I have encountered this issue when I was searching for a solution for exact same problem. It looks that this solution only works if you provide your own UniqueTogetherValidation in the serializer's meta validators field. While this is OK, without setting
The code will raise KeyError. Is this the desired behavior? |
Hi @fatalispm. I think you're running into #7143, which hasn't been released yet. |
* Add failing tests for unique_together+source * Fix UniqueTogetherValidator source handling * Fix read-only+default+source handling * Update test to use functional serializer * Test UniqueTogetherValidator error+source
* Add failing tests for unique_together+source * Fix UniqueTogetherValidator source handling * Fix read-only+default+source handling * Update test to use functional serializer * Test UniqueTogetherValidator error+source
This is an updated version of #7005 that I was helping @anveshagarwal put together. In short, there are two related bugs:
_read_only_defaults
does not correctly account for the fieldsource
. The initial check does account for compatible sources, but when assigning the read only default, thefield_name
is used instead ofsource
.UniqueTogetherValidator
doesn't handle field sources. Handling this is a little annoying, because the objective is to validate the serializer fields/data. However, the givenattrs
use the source/model field names, which are used in the actual validation check.As to potential breaking changes, we may want to call out that validation against read-only fields that have defaults now obey their sources? ¯\_(ツ)_/¯
Fixes #7003