For one of my models, I need to ensure the unicity of some rows, but only in a certain case. Only the \"validated\" rows should follow this constraint.
Basically, I\'
In addition to the previous answer you can overwrite the save()
method. It would be something like this:
def save(self, **kwargs):
try:
self.objects.get(field_a=self.field_a, field_b=self.field_b, validated=True)
# The object already exist therefore throw an exception
raise ValidationError(
"field_a and field_b must be unique if validated=True"
)
except self.__class__.DoesNotExist: # Save the model
super(MyModel, self).save(**kwargs) # inherit and call the save method
Now you don't need to call the clean()
method.