I've overwritten the clean() method to perform a custom validation in my model. Is there any way to get the instance of the model that is being saved?
class Consumption(models.Model):
storage = models.ForeignKey(Storage, related_name='consumption')
timestamp = UnixDateTimeField()
consumption = models.BigIntegerField(help_text='Consumption in Bytes')
def clean(self):
if self.storage_type == PRIMARY:
if Storage.objects.filter(company=self.company, storage_type=PRIMARY).exists():
raise ValidationError({'storage_type': 'Already exists a Primary storage'})
When I modify an Storage, which is related to a consumption, it raises the ValidationError. So I'd like to improve the filter like this:
Storage.objects.exclude(pk=self.instance.pk).filter(...)
Where can I take the instance from?