私は自分のフォームにクリーンメソッドを書いており、実際に検証を行っていません。私のフォームのクリーンメソッドがきれいにならないのはなぜですか?
class Property1Form(forms.ModelForm):
class Meta:
model = Property1
fields = ['unit','propertytype','is_true','date','followup_date','quantity','description']
def __init__(self, *args, **kwargs):
super(Property1Form, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance:
self.fields['unit'].required = False
self.fields['unit'].widget.attrs['disabled'] = 'disabled'
def clean(self):
form_data = self.cleaned_data
if Property1.objects.filter(unit=form_data['unit'], propertytype=form_data['propertytype']).count() > 0:
self._errors["propertytype"] = ["Propertytype already exists for unit"] # Will raise a error message
del form_data['propertytype']
同じ検証は、モデルレベルで私のために動作しませんが、モデルのレベルで私は /ユニット/プロパティで500エラー
ValidationErrorをを取得しています/新しい/ 6/http://127.0.0.1:8000/unit/property_details/6/ {」すべて ':[u'Sameプロパティは複数のものを割り当てることはできません]
フォーム側でも同じ検証をしようとしています。
UPDATE:あなたはrequired=False
を設定し、unit
フィールドを無効にしている
def property_new(request,pk,uri):
unit = get_object_or_404(Unit, pk=pk)
title = 'property'
uri = _get_redirect_url(request, uri)
if request.method == "POST":
form = Property1Form(request.POST)
form.fields['unit'] = unit
if form.is_valid():
properties = form.save(commit=False)
properties.unit = unit
properties.save()
messages.add_message(request, messages.SUCCESS, str(properties.unit) + "-SUCCESS Object created sucssefully")
return redirect(uri)
else:
form = Property1Form(initial={'unit': unit})
return render(request, 'object_edit.html', {'form': form, 'title':title, 'extend': EXTEND})
ビューコードを追加し、エラー –
@ trader of @SardorbekImomalievをビューコードで更新しました。私はエラーが発生していないため、トレースログはありません。 –