更新时引发 ValidationError
ValidationError raises when updating
我定义了一个ValidationError
。它工作正常但是当我尝试更新我的表单时它会出现。因此,我无法再更新我的表格。定义如下:
def clean_examination(self):
new_exa = self.cleaned_data.get('examination')
try:
old_exa=GeneralData.objects.get(examination=self.cleaned_data.get('examination'))
except GeneralData.DoesNotExist:
return new_exa
if old_exa:
raise forms.ValidationError('Object with this examination already exists!')
return new_exa`
是否可以 "deactivate" 我的 ValidationError
如果表格需要更新?
附加信息:
我的模特:
class GeneralData(models.Model):
examination = models.ForeignKey(Examination, on_delete=models.CASCADE, help_text='This value is pasted in automatically and is not editable!')
attr1= models.FloatField()
attr2= models.FloatField()
我的更新视图:
class GeneralDataUpdate(LoginRequiredMixin, generic.UpdateView):
model = GeneralData
form_class = GeneralDataCreateForm
template_name_suffix = '_update_form'
login_url = 'member:login'
def get_success_url(self):
return reverse('member:detail', kwargs={'pk': self.get_object().examination.patient_id})
def get_context_data(self, **kwargs):
context = super(GeneralDataUpdate, self).get_context_data(**kwargs)
context['action'] = reverse('member:generaldata-update',
kwargs={'pk': self.get_object().id})
return context
我的更新-URL:
url(r'examination/(?P<pk>[0-9]+)/generaldata/update/$', views.GeneralDataUpdate.as_view(), name='generaldata-update')
如果您的表单来自 ModelForm
类型,则您有一个属性 self.instance
。然后你可以检查是否设置了 self.instance.id
,并处理你的验证。
if self.instance.id:
# raise ValidationError
如果你不使用它,应该清除 id,它应该与请求一起发送。存在则抛出ValidationError
,否则继续..
id = self.cleaned_data.get('id', None)
if id:
# raise ValidationError
示例:
def clean_examination(self):
new_exa = self.cleaned_data.get('examination')
try:
old_exa = GeneralData.objects.get(
examination=self.cleaned_data.get('examination'))
except GeneralData.DoesNotExist:
return new_exa
if old_exa:
if not self.instance.id:
raise forms.ValidationError(
'Object with this examination already exists!')
return new_exa
在这种情况下,如果没有旧对象,ValidationError
只会在有旧对象时引发examination
。
请注意,如果该对象有一个 id
,更改为已存在的具有 OTHER id
的名称,您将有两个不同的对象进行相同的检查。不确定是否可以,但这当然取决于您。
我定义了一个ValidationError
。它工作正常但是当我尝试更新我的表单时它会出现。因此,我无法再更新我的表格。定义如下:
def clean_examination(self):
new_exa = self.cleaned_data.get('examination')
try:
old_exa=GeneralData.objects.get(examination=self.cleaned_data.get('examination'))
except GeneralData.DoesNotExist:
return new_exa
if old_exa:
raise forms.ValidationError('Object with this examination already exists!')
return new_exa`
是否可以 "deactivate" 我的 ValidationError
如果表格需要更新?
附加信息: 我的模特:
class GeneralData(models.Model):
examination = models.ForeignKey(Examination, on_delete=models.CASCADE, help_text='This value is pasted in automatically and is not editable!')
attr1= models.FloatField()
attr2= models.FloatField()
我的更新视图:
class GeneralDataUpdate(LoginRequiredMixin, generic.UpdateView):
model = GeneralData
form_class = GeneralDataCreateForm
template_name_suffix = '_update_form'
login_url = 'member:login'
def get_success_url(self):
return reverse('member:detail', kwargs={'pk': self.get_object().examination.patient_id})
def get_context_data(self, **kwargs):
context = super(GeneralDataUpdate, self).get_context_data(**kwargs)
context['action'] = reverse('member:generaldata-update',
kwargs={'pk': self.get_object().id})
return context
我的更新-URL:
url(r'examination/(?P<pk>[0-9]+)/generaldata/update/$', views.GeneralDataUpdate.as_view(), name='generaldata-update')
如果您的表单来自 ModelForm
类型,则您有一个属性 self.instance
。然后你可以检查是否设置了 self.instance.id
,并处理你的验证。
if self.instance.id:
# raise ValidationError
如果你不使用它,应该清除 id,它应该与请求一起发送。存在则抛出ValidationError
,否则继续..
id = self.cleaned_data.get('id', None)
if id:
# raise ValidationError
示例:
def clean_examination(self):
new_exa = self.cleaned_data.get('examination')
try:
old_exa = GeneralData.objects.get(
examination=self.cleaned_data.get('examination'))
except GeneralData.DoesNotExist:
return new_exa
if old_exa:
if not self.instance.id:
raise forms.ValidationError(
'Object with this examination already exists!')
return new_exa
在这种情况下,如果没有旧对象,ValidationError
只会在有旧对象时引发examination
。
请注意,如果该对象有一个 id
,更改为已存在的具有 OTHER id
的名称,您将有两个不同的对象进行相同的检查。不确定是否可以,但这当然取决于您。