폼에 대한 첫 번째 유효성 검사기를 수행했습니다.이 작업이 수행되는 방식이 아닌 것 같습니다.django 폼 유효성 검사가 잘못된 코드처럼 보입니다.
작동하지만 추악한 코드처럼 보입니다. 누군가 도움을 줄 수 있습니까?
내 양식 :
class CountryForm(forms.ModelForm):
"""Form to create or edit Countries."""
class Meta:
model = Country
def clean_name(self):
raw_data = self.cleaned_data['name']
data = raw_data.title()
try:
Province.objects.get(name=data).exists()
raise forms.ValidationError("There is already a province with the name: %s") % data
except:
pass
try:
Region.objects.get(name=data).exists()
raise forms.ValidationError("There is already a region with the name: %s") % data
except:
pass
return data
내 생각은 이름이 다른 두 모델에 이름으로 존재하는지 확인하고 수행 할 때 오류가 발생하는 것입니다.
고마워요, 훨씬 더 명확하게 :) –
하지만 내가 어디에 있는지 모르겠다 (form.instance 및 form.instance.pk) : 만약 내가 들여 쓰기와 함께 2 다른 수표 전에 생각하지만, 그게 나던 속임수를 쓰는 것 같다. –