类似对象的表单确认ValidationError
Form confirmation ValidationError for similar objects
在保存对象之前,我想添加一条验证警告消息,以询问最终用户 he/she 是否要继续,给定从数据库返回的类似数据。
例如,当最终用户正在添加一个 Hospital
,并且一个或多个 Hospital
对象已经存在于那个 zipcode
中,那么 ValidationError
应该是提供类似 Following hospitals in zipcode XX were already found: YYY, ZZZZ. Hit submit again to save if you're sure you want to add another one.
的说法
我试图通过添加字段 Verified
(设置为 HiddenInput
)使用以下表格来实现此目的。看来我无法修改该字段。
class CreateHospitalForm(forms.ModelForm):
verified = forms.BooleanField(widget=forms.HiddenInput, required=False)
def clean(self):
cleaned_data = self.cleaned_data
zipcode = cleaned_data.get('zipcode')
verified = cleaned_data.get('verified')
existing_hospitals = Hospital.objects.filter(zipcode=zipcode)
if existing_hospitals and not verified:
cleaned_data['verified'] = True # <<< here I'd like to set verified to True so I don't get the below warning a 2nd time when saving the form
raise ValidationError(u"Following hospitals already exist in this zipcode: " + ', '.join([str(x) for x in existing_hospitals] + ". Are you sure you want to add another one? Then hit submit again.")
return cleaned_data
class Meta:
model = Hospital
fields = ('name', 'street_and_number', 'zipcode', 'town', 'country', 'email', 'web', 'comments', 'verified')
知道如何解决这个问题吗?
使用 Django 1.7.
编辑:我的观点是非常标准的,按照要求在下面:
class CreateHospitalView(generic.CreateView):
model = Hospital
template_name = 'hosp_add.html'
form_class = CreateHospitalForm
def get_success_url(self):
return reverse('hosp_detail', args=(self.object.id,))
这是你的答案。将您的清理方法更新为此(请参阅代码中的注释)。
def clean(self):
cleaned_data = self.cleaned_data
zipcode = cleaned_data.get('zipcode')
verified = cleaned_data.get('verified')
existing_hospitals = Hospital.objects.filter(zipcode=zipcode)
if existing_hospitals and not verified:
self.data = self.data.copy() # self.data is immutable, so we make a copy to get around this
self.data['verified'] = True # instead of self.cleaned_data['verified'] = True
raise ValidationError(u"Following hospitals already exist in this zipcode: " + ', '.join([str(x) for x in existing_hospitals] + ". Are you sure you want to add another one? Then hit submit again.")
return cleaned_data
在保存对象之前,我想添加一条验证警告消息,以询问最终用户 he/she 是否要继续,给定从数据库返回的类似数据。
例如,当最终用户正在添加一个 Hospital
,并且一个或多个 Hospital
对象已经存在于那个 zipcode
中,那么 ValidationError
应该是提供类似 Following hospitals in zipcode XX were already found: YYY, ZZZZ. Hit submit again to save if you're sure you want to add another one.
我试图通过添加字段 Verified
(设置为 HiddenInput
)使用以下表格来实现此目的。看来我无法修改该字段。
class CreateHospitalForm(forms.ModelForm):
verified = forms.BooleanField(widget=forms.HiddenInput, required=False)
def clean(self):
cleaned_data = self.cleaned_data
zipcode = cleaned_data.get('zipcode')
verified = cleaned_data.get('verified')
existing_hospitals = Hospital.objects.filter(zipcode=zipcode)
if existing_hospitals and not verified:
cleaned_data['verified'] = True # <<< here I'd like to set verified to True so I don't get the below warning a 2nd time when saving the form
raise ValidationError(u"Following hospitals already exist in this zipcode: " + ', '.join([str(x) for x in existing_hospitals] + ". Are you sure you want to add another one? Then hit submit again.")
return cleaned_data
class Meta:
model = Hospital
fields = ('name', 'street_and_number', 'zipcode', 'town', 'country', 'email', 'web', 'comments', 'verified')
知道如何解决这个问题吗? 使用 Django 1.7.
编辑:我的观点是非常标准的,按照要求在下面:
class CreateHospitalView(generic.CreateView):
model = Hospital
template_name = 'hosp_add.html'
form_class = CreateHospitalForm
def get_success_url(self):
return reverse('hosp_detail', args=(self.object.id,))
这是你的答案。将您的清理方法更新为此(请参阅代码中的注释)。
def clean(self):
cleaned_data = self.cleaned_data
zipcode = cleaned_data.get('zipcode')
verified = cleaned_data.get('verified')
existing_hospitals = Hospital.objects.filter(zipcode=zipcode)
if existing_hospitals and not verified:
self.data = self.data.copy() # self.data is immutable, so we make a copy to get around this
self.data['verified'] = True # instead of self.cleaned_data['verified'] = True
raise ValidationError(u"Following hospitals already exist in this zipcode: " + ', '.join([str(x) for x in existing_hospitals] + ". Are you sure you want to add another one? Then hit submit again.")
return cleaned_data