mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-29 17:39:48 +03:00
Fix UniqueTogetherValidator source handling
This commit is contained in:
parent
346f00f55e
commit
87cba7b814
|
@ -106,7 +106,7 @@ class UniqueTogetherValidator:
|
||||||
missing_items = {
|
missing_items = {
|
||||||
field_name: self.missing_message
|
field_name: self.missing_message
|
||||||
for field_name in self.fields
|
for field_name in self.fields
|
||||||
if field_name not in attrs
|
if serializer.fields[field_name].source not in attrs
|
||||||
}
|
}
|
||||||
if missing_items:
|
if missing_items:
|
||||||
raise ValidationError(missing_items, code='required')
|
raise ValidationError(missing_items, code='required')
|
||||||
|
@ -115,17 +115,23 @@ class UniqueTogetherValidator:
|
||||||
"""
|
"""
|
||||||
Filter the queryset to all instances matching the given attributes.
|
Filter the queryset to all instances matching the given attributes.
|
||||||
"""
|
"""
|
||||||
|
# field names => field sources
|
||||||
|
sources = [
|
||||||
|
serializer.fields[field_name].source
|
||||||
|
for field_name in self.fields
|
||||||
|
]
|
||||||
|
|
||||||
# If this is an update, then any unprovided field should
|
# If this is an update, then any unprovided field should
|
||||||
# have it's value set based on the existing instance attribute.
|
# have it's value set based on the existing instance attribute.
|
||||||
if serializer.instance is not None:
|
if serializer.instance is not None:
|
||||||
for field_name in self.fields:
|
for source in sources:
|
||||||
if field_name not in attrs:
|
if source not in attrs:
|
||||||
attrs[field_name] = getattr(serializer.instance, field_name)
|
attrs[source] = getattr(serializer.instance, source)
|
||||||
|
|
||||||
# Determine the filter keyword arguments and filter the queryset.
|
# Determine the filter keyword arguments and filter the queryset.
|
||||||
filter_kwargs = {
|
filter_kwargs = {
|
||||||
field_name: attrs[field_name]
|
source: attrs[source]
|
||||||
for field_name in self.fields
|
for source in sources
|
||||||
}
|
}
|
||||||
return qs_filter(queryset, **filter_kwargs)
|
return qs_filter(queryset, **filter_kwargs)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user