-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added lazy unique together validator
- Loading branch information
1 parent
cd8c556
commit d70567a
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |