Fix UniqueTogetherValidator with field sources#7086
Fix UniqueTogetherValidator with field sources#7086lovelydinosaur merged 5 commits intoencode:masterfrom
Conversation
|
Hey @rpkilby. Thanks for this. Tricky stuff. 🌶 Let me give it a look over. (But first glance it seems clean.) |
carltongibson
left a comment
There was a problem hiding this comment.
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_defaultsdoes not correctly account for the fieldsource. The initial check does account for compatible sources, but when assigning the read only default, thefield_nameis used instead ofsource.UniqueTogetherValidatordoesn't handle field sources. Handling this is a little annoying, because the objective is to validate the serializer fields/data. However, the givenattrsuse 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