1010from django .utils .translation import gettext_lazy as _
1111from rest_framework import serializers
1212from rest_framework .exceptions import ValidationError
13- from rest_framework .validators import UniqueValidator
13+ from rest_framework .validators import UniqueTogetherValidator , UniqueValidator
1414
1515
1616class BaseNestedModelSerializer (serializers .ModelSerializer ):
@@ -400,12 +400,14 @@ class Meta:
400400 (`UniqueFieldsMixin` and `NestedCreateMixin` or `NestedUpdateMixin`)
401401 you should put `UniqueFieldsMixin` ahead.
402402 """
403- _unique_fields = [] # type: List[Tuple[str,UniqueValidator]]
403+ _unique_fields = [] # type: List[Tuple[str, UniqueValidator]]
404+ _unique_together_validators = [] # type: List[UniqueTogetherValidator]
404405
405406 def get_fields (self ):
406407 self ._unique_fields = []
407408
408409 fields = super (UniqueFieldsMixin , self ).get_fields ()
410+
409411 for field_name , field in fields .items ():
410412 unique_validators = [validator
411413 for validator in field .validators
@@ -419,6 +421,10 @@ def get_fields(self):
419421
420422 return fields
421423
424+ def get_unique_together_validators (self ):
425+ self ._unique_together_validators = super ().get_unique_together_validators ()
426+ return []
427+
422428 def _validate_unique_fields (self , validated_data ):
423429 for unique_field in self ._unique_fields :
424430 field_name , unique_validator = unique_field
@@ -434,6 +440,8 @@ def _validate_unique_fields(self, validated_data):
434440 self .fields [field_name ])
435441 except ValidationError as exc :
436442 raise ValidationError ({field_name : exc .detail })
443+ for validator in self ._unique_together_validators :
444+ validator (validated_data , self )
437445
438446 def create (self , validated_data ):
439447 self ._validate_unique_fields (validated_data )
0 commit comments