13
Iβve found a solution to override method:
class CommonMeasurement(models.Model):
timestamp = models.DateTimeField()
value = models.FloatField()
run = models.ForeignKey(Run)
objects = models.Manager()
analyzes = managers.MeasureStatManager()
def save(self, **kwargs):
self.clean()
return super(CommonMeasurement, self).save(**kwargs)
def clean(self):
super(CommonMeasurement, self).clean()
print 'here we go'
if self.timestamp < self.run.timestamp_start or self.timestamp > self.run.timestamp_end:
raise django_excetions.ValidationError('Measurement is outside the run')
But Iβm not sure that it can be a good decision.
53
To call the model clean method we will override save method. Check the link: https://docs.djangoproject.com/en/2.0/ref/models/instances/#django.db.models.Model.clean
class CommonMeasurement(models.Model):
timestamp = models.DateTimeField()
value = models.FloatField()
run = models.ForeignKey(Run)
def clean(self):
if self.timestamp < self.run.timestamp_start or self.timestamp > self.run.timestamp_end:
raise django_excetions.ValidationError('Measurement is outside the run')
def save(self, *args, **kwargs):
self.full_clean()
return super(CommonMeasurement, self).save(*args, **kwargs)
- [Django]-Group by Foreign Key and show related items β Django
- [Django]-Django gunicorn sock file not created by wsgi
- [Django]-Multiple level template inheritance in Jinja2?
7
Apparently model.clean() is never called to ensure backward compatibility. For more information on this: https://code.djangoproject.com/ticket/13100
- [Django]-Distributing Django projects with unique SECRET_KEYs
- [Django]-Django admin TabularInline β is there a good way of adding a custom html column?
- [Django]-Why is logged_out.html not overriding in django registration?
2
Apparently in newer versions of Django the ModelForm.full_clean
does call itβs instanceβs full_clean
method:
- [Django]-Django queryset filter for blank FileField?
- [Django]-Django print choices value
- [Django]-Accessing the user's request in a post_save signal
Source:stackexchange.com