0
을 장고 :패스 데이터는이 같은 형태가 폼의 필드 청소 방법
class TitlePropose(forms.Form):
title = forms.CharField(max_length=128)
code= forms.CharField(max_length=32)
def __init__(self, contest, *args, **kwargs):
super(TitlePropose, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = self.__class__.__name__.lower()
self.helper.form_action = ''
self.helper.layout = Layout(,
Field('title'),
Field('code'),
)
def clean_title(self):
if OtherModel.objects.filter(contest=contest, title=self.cleaned_data['title']).count() > 0:
raise forms.ValidationError("Title unavailable")
else:
return self.cleaned_data['title']
내가 어떤 성공을하지 않고, clean_title 방법에서 변수 "대회"에 접근을 시도합니다. 양식 클래스 생성자에서이 변수를 전달합니다.
#contest is just some object
new_title_form = TitlePropose(contest=contest.uuid)
제안 사항, clean_title에서 'contest'에 액세스하려면 어떻게해야합니까?
이를 참조하십시오 https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other – karthikr