Python Django-使用full_clean()验证管理错误
我使用modelformset_factory,并使用full_clean()验证表单,其中unique_together=True。我想知道,如果为了在模板中返回错误消息,唯一的_一起无法验证,那么处理错误的最佳方法是什么 请看看我的观点,告诉我我的做法是否正确,或者是否有更好的方法 型号:Python Django-使用full_clean()验证管理错误,python,django,django-forms,django-views,Python,Django,Django Forms,Django Views,我使用modelformset_factory,并使用full_clean()验证表单,其中unique_together=True。我想知道,如果为了在模板中返回错误消息,唯一的_一起无法验证,那么处理错误的最佳方法是什么 请看看我的观点,告诉我我的做法是否正确,或者是否有更好的方法 型号: class Attribute(models.Model): shapefile = models.ForeignKey(Shapefile) name = models.CharFiel
class Attribute(models.Model):
shapefile = models.ForeignKey(Shapefile)
name = models.CharField(max_length=255, db_index=True)
type = models.IntegerField()
width = models.IntegerField()
precision = models.IntegerField()
def __unicode__(self):
return self.name
def delete(self):
shapefile = self.shapefile
feature_selected = Feature.objectshstore.filter(shapefile=shapefile)
feature_selected.hremove('attribute_value', self.name)
super(Attribute, self).delete()
class Meta:
unique_together = (('name', 'shapefile'),)
表格:
视图:
这种方法的缺点是将验证从表单移动到视图 最近,我遇到了同样的问题,即验证一个唯一的集合约束,其中一个字段被排除在模型表单之外。我的解决方案是重写模型表单的
clean
方法,并查询数据库以检查unique-together约束。这重复了full\u clean
调用的代码,但我喜欢它,因为它是显式的
我曾短暂地考虑过重写更为枯燥的内容,但我决定不依赖私有api。这种方法的缺点是将验证从表单移动到视图 最近,我遇到了同样的问题,即验证一个唯一的集合约束,其中一个字段被排除在模型表单之外。我的解决方案是重写模型表单的
clean
方法,并查询数据库以检查unique-together约束。这重复了full\u clean
调用的代码,但我喜欢它,因为它是显式的
我曾短暂地考虑过重写,这会更加枯燥,但我决定不依赖于私有api。请注意,您不需要使用行
instance=getattr(self,'instance',None)
。调用超类“\uuuu init\uuuu
方法后,您可以只使用self.instance
,尽管其值可能为None。请注意,您不需要行instance=getattr(self,'instance',None)
。调用超类“\uuuu init\uuuu
方法后,您可以只使用self.instance
,尽管其值可能为无。
class AttributeForm(ModelForm):
def __init__(self, *args, **kwargs):
super(AttributeForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance and instance.pk:
self.fields['type'].widget.attrs['disabled'] = True
self.fields['type'].required = False
self.fields['width'].widget.attrs['readonly'] = True
self.fields['precision'].widget.attrs['readonly'] = True
def clean_type(self):
if self.instance and self.instance.pk:
return self.instance.type
else:
return self.cleaned_data['type']
type = forms.ChoiceField(choices=FIELD_TYPE)
class Meta:
model = Attribute
exclude = 'shapefile'
def editFields(request, shapefile_id):
layer_selected = Shapefile.objects.get(pk=shapefile_id)
attributes_selected= Attribute.objects.filter(shapefile__pk=shapefile_id)
attributesFormset = modelformset_factory(Attribute, form=AttributeForm, extra=1, can_delete=True)
if request.POST:
formset = attributesFormset(request.POST, queryset=attributes_selected)
if formset.is_valid():
instances = formset.save(commit=False)
for instance in instances:
instance.shapefile = layer_selected
try:
instance.full_clean()
except ValidationError as e:
non_field_errors = e.message_dict[NON_FIELD_ERRORS]
print non_field_errors
formset = attributesFormset(queryset=attributes_selected)
return render_to_response("basqui/manage_layer_editFields.html", {'shapefile': layer_selected, 'formset':formset}, context_instance=RequestContext(request))
instance.save()
formset = attributesFormset(queryset=attributes_selected)
return render_to_response("basqui/manage_layer_editFields.html", {'shapefile': layer_selected, 'formset':formset}, context_instance=RequestContext(request))