0
我的表單上有一個名爲location
的域,我不需要預先加載所有選項,因爲它需要很長時間才能呈現。所以這是我的:Django form 10M選項的ModelMultipleChoiceField
class SearchForm(forms.Form):
location = forms.ModelMultipleChoiceField(
required=False,
label='Location',
queryset=Location.objects.none()
)
而且我提供了一個使用ajax的自動完成功能。問題是表單驗證失敗,因爲所選值不在queryset
中。有沒有辦法可以跳過location
的驗證或類似的東西?
編輯:
我找到了一種方法,但我不認爲這是最好的選擇:
class SearchForm(forms.Form):
location = forms.MultipleChoiceField(
required=False,
label='Location',
validators=[]
)
我加validators = []
到我的位置字段,改變ModelMultipleChoiceField
到MultipleChoiceField
。現在,在我的save
功能,我有這樣的:
def save(self):
if not self.is_valid():
return false
locations = Location.objects.filter(id__in=self.data.getlist('location')).all()
由於locations
不會在我的cleaned_data
,我只是用在data
傳遞的位置ID查詢數據庫。
我仍然不認爲這是一個乾淨的方式,所以任何其他解決方案將不勝感激。
感謝。我會檢查出來的。但我想找到一種解決方法,無需任何外部插件。 – AliBZ