-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
UniqueTogetherValidator
does not work with source
kwarg
#9442
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
Comments
UniqueTogetherValidator
does not work with source
attrUniqueTogetherValidator
does not work with source
kwarg
I tried to reproduce the same and I am able to get the expected results. My code : - class PeopleSerialiser(serializers.Serializer):
Output : { The only limitation I see here is, it throws this error only when you try to post the names in the same order. If this does not resolve your issue, please provide the code of your model class that you are trying to serialize. Also, specify the name of the model class. I'll dig deeper. |
When I have the time I'll try to open a PR adding a failing test case for this scenario. |
can you both review this PR #9482, please? |
My team has run into a bug in
UniqueTogetherValidator
where it does not work properly with fields with thesource
attribute. If a field has asource
that is different than the field's name on the serializer, then that field is effectively ignored when checking for uniqueness violations, and theValidationError
is not raised.I am using a serializer set up like this. Note that
UniqueTogetherValidator
is expecting the serializer's name for thefields
argument— you'll get an error on this line if you try to provide thesource
name instead.I believe the bug occurs at this position in the code:
django-rest-framework/rest_framework/validators.py
Lines 167 to 178 in e13688f
On line 172, we check if
field
(which comes fromsource
) is inself.fields
(which is the name on the serializer). If we're looking at the serializer above, thenattrs.keys()
would be['list', 'ordering_key']
andself.fields
is['list_id', 'position']
.This means that
checked_values
will always be empty, and aValidationError
will never be raised.Checklist
REST Framework version: 3.15.1
The text was updated successfully, but these errors were encountered: