I have a view, which creates models from CSV file. I've added clean method to the model class definition, but it isn't called when model is created.
我有一个视图,它从CSV文件创建模型。我已经为模型类定义添加了clean方法,但是在创建模型时不会调用它。
Here is example of models.py:
这是models.py的示例:
class Run(models.Model):
name = models.CharField(max_length=120)
tested_build = models.ForeignKey('apps.Build')
timestamp_start = models.DateTimeField()
timestamp_end = models.DateTimeField()
class CommonMeasurement(models.Model):
timestamp = models.DateTimeField()
value = models.FloatField()
run = models.ForeignKey(Run)
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')
class ClientMeasurement(CommonMeasurement):
metric = models.ForeignKey(ClientMetric)
account = models.CharField(max_length=120, blank=True)
Here is my Form View code examples:
这是我的表单视图代码示例:
class BaseMeasurementsUpload(generic_views.FormView):
template_name = 'upload.html'
models_to_upload = None
def get_success_url(self):
return self.request.get_full_path()
def form_valid(self, form):
uploader = getattr(importers, form.cleaned_data['uploader'])
try:
the_uploader = uploader(form.cleaned_data, self.models_to_upload)
upload_results = the_uploader.get_result_info()
except django_exceptions.ValidationError as e:
custom_errors = e
return render_to_response(self.template_name,
{'upload_results': upload_results,
'custom_errors': custom_errors},
context_instance=RequestContext(self.request))
class ClientMeasurementsUploadView(BaseMeasurementsUpload):
form_class = forms.ClientMeasurementsUploadForm
models_to_upload = models.ClientMeasurement
def get_form(self, form_class):
uploaders = (('MeasurementsSimpleCsv', importers.MeasurementsSimpleCsv.__doc__),
('ClientMeasurementsBulkCsv', importers.ClientMeasurementsBulkCsv.__doc__,))
if self.request.POST:
# get bound form
return self.form_class(uploaders,
self.request.POST,
self.request.FILES)
else:
return forms.ClientMeasurementsUploadForm(uploaders)
importers perform actual validation and call create method for each model.
导入器执行实际验证并为每个模型调用create方法。
4 个解决方案
#1
15
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
要调用模型清理方法,我们将覆盖save方法。检查链接: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)
#2
6
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.
但我不确定这是一个好的决定。
#3
3
Apparently model.clean() is never called to ensure backward compatibility. For more information on this: https://code.djangoproject.com/ticket/13100
显然,从不调用model.clean()来确保向后兼容性。有关这方面的更多信息:https://code.djangoproject.com/ticket/13100
#4
2
Apparently in newer versions of Django the ModelForm.full_clean
does call it's instance's full_clean
method:
显然在较新版本的Django中,ModelForm.full_clean会调用它的实例的full_clean方法:
- https://github.com/django/django/blob/master/django/forms/models.py#L396
- https://github.com/django/django/blob/master/django/forms/models.py#L396
#1
15
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
要调用模型清理方法,我们将覆盖save方法。检查链接: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)
#2
6
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.
但我不确定这是一个好的决定。
#3
3
Apparently model.clean() is never called to ensure backward compatibility. For more information on this: https://code.djangoproject.com/ticket/13100
显然,从不调用model.clean()来确保向后兼容性。有关这方面的更多信息:https://code.djangoproject.com/ticket/13100
#4
2
Apparently in newer versions of Django the ModelForm.full_clean
does call it's instance's full_clean
method:
显然在较新版本的Django中,ModelForm.full_clean会调用它的实例的full_clean方法:
- https://github.com/django/django/blob/master/django/forms/models.py#L396
- https://github.com/django/django/blob/master/django/forms/models.py#L396