aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/validators.py
diff options
context:
space:
mode:
authorTom Christie2014-11-19 13:55:10 +0000
committerTom Christie2014-11-19 13:55:10 +0000
commit8586290df80ac8448d71cdb3326bc822c399cad1 (patch)
tree25b657c1bf29547c9380816d5fc8b4f42854ceba /rest_framework/validators.py
parent6cb6510132b319c96b28bea732032aaf2d495895 (diff)
downloaddjango-rest-framework-8586290df80ac8448d71cdb3326bc822c399cad1.tar.bz2
Apply defaults and requiredness to unique_together fields. Closes #2092.
Diffstat (limited to 'rest_framework/validators.py')
-rw-r--r--rest_framework/validators.py14
1 files changed, 13 insertions, 1 deletions
diff --git a/rest_framework/validators.py b/rest_framework/validators.py
index fa4f1847..7ca4e6a9 100644
--- a/rest_framework/validators.py
+++ b/rest_framework/validators.py
@@ -93,6 +93,9 @@ class UniqueTogetherValidator:
The `UniqueTogetherValidator` always forces an implied 'required'
state on the fields it applies to.
"""
+ if self.instance is not None:
+ return
+
missing = dict([
(field_name, self.missing_message)
for field_name in self.fields
@@ -105,8 +108,17 @@ class UniqueTogetherValidator:
"""
Filter the queryset to all instances matching the given attributes.
"""
+ # If this is an update, then any unprovided field should
+ # have it's value set based on the existing instance attribute.
+ if self.instance is not None:
+ for field_name in self.fields:
+ if field_name not in attrs:
+ attrs[field_name] = getattr(self.instance, field_name)
+
+ # Determine the filter keyword arguments and filter the queryset.
filter_kwargs = dict([
- (field_name, attrs[field_name]) for field_name in self.fields
+ (field_name, attrs[field_name])
+ for field_name in self.fields
])
return queryset.filter(**filter_kwargs)