i use django-reversion to controll changes of my models but i have a problem with the models clean method.
For example my model looks like this:
class Document(models.Model):
name = models.CharField(max_length=255, blank=True, null=True)
client = models.ForeignKey(Client, on_delete=models.CASCADE)
template = models.ForeignKey(Template, on_delete=models.CASCADE)
def clean(self):
if self.template.only_one and (Document.objects.filter(client=self.client, template=self.template).count() > 0):
raise ValidationError('Test')
I register this model in the admin.py:
@admin.register(Document)
class DocumentReversion(VersionAdmin):
pass
Now i create a record for Document in the admin section referencing to a template where the field only_one
is True
.
If i delete this record and retrieve it, a ValidationError fires.
Why? Because i have deleted the only available record. Very curious...