When I save my form I perform validation of data that is defined in my model
def clean(self):
model = self.__class__
if self.unit and (self.is_active == True) and model.objects.filter(unit=self.unit, is_terminated = False , is_active = True).exclude(id=self.id).count() > 0:
raise ValidationError('Unit has active lease already, Terminate existing one prior to creation of new one or create a not active lease '.format(self.unit))
How can I trigger same clean method during simple update without a need to duplicate clean logic in my update view?(In my view I just perform update without any form)
Unit.objects.filter(pk=term.id).update(is_active=False)
updatedon't call thesavemethod of your model and so it's not possible for django to raiseValidationErrorexceptions in this case.You need to at least call the
full_cleanmethod of your model before doing the update.Maybe like this ?
Reference: https://docs.djangoproject.com/en/1.11/ref/models/instances/#validating-objects