Skip to content

Commit

Permalink
added lazy unique together validator
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartaccent committed Mar 16, 2018
1 parent cd8c556 commit d70567a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions rest_serializers/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from rest_framework import serializers


class LazyUniqueTogetherValidator(serializers.UniqueTogetherValidator):
"""
A unique together validator that will only check if all fields exist.
This allows a missing field being added programmatically after the
initial validate.
"""

def has_missing_fields(self, attrs):
for field_name in self.fields:
if field_name not in attrs:
return True

def __call__(self, attrs):
# only allow it to continue if all fields are present
if self.has_missing_fields(attrs):
return
super().__call__(attrs)

0 comments on commit d70567a

Please sign in to comment.