forked from django-import-export/django-import-export
-
Notifications
You must be signed in to change notification settings - Fork 0
Get field constraints (handle model constraints) #4
Copy link
Copy link
Open
Description
def validate_unique(self, values):
'''
Perform a check on possible existing objects
if the field has a unique constraint
Return True if validated, else False
'''
values = [val for val in values if val]
unique_values = set(values)
# Check if all values are unique
if len(values) != len(unique_values):
return False
# Check if values are not already in database
lookups = [
models.Q(**{self.field: val})
for val in unique_values
if val
]
lookup = reduce(operator.or_, lookups)
return not self.model.objects.filter(lookup).exists()
widgets.ForeignKeyWidget.validate_unique is not implemented yet because we need to know if the field linked with the widget has a unique constraint or not.
If this is the case, we run validate_unique to prevent import when clauses are not respected (avoid IntegrityError exceptions).
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels