0
我有这个当前的形式:Django的ModelChoiceField,验证动态查询集
class PersonForm(forms.Form):
article = forms.CharField(required=False)
workshop = forms.ModelChoiceField(queryset=Program.objects.none(),
empty_label="----",
required=False,
label='Atelier')
def __init__(self, *args, **kwargs):
super(PersonForm, self).__init__(*args, **kwargs)
article = self.initial.get('article', None)
if article:
a = Article.objects.get(pk=article)
if a.workshop:
self.fields['workshop'].queryset = Program.objects.filter(event=a.event, workshop=True)
self.fields['workshop'].required = True
self.helper.layout.insert(4, Row1Field('workshop',))
用于检索所有讲习班的查询集是动态的,所以ModelChoiceField内的查询集属性设置为Program.objects.none()
,动态查询集在窗体的内部完成__init__
方法
一切工作正常:在选择内显示workshop
的所有值都可以。但是当我发布表单时,出现验证错误:'Select a valid choice. This choice is not among available choices.'
有什么我失踪了吗?
请显示您的看法。 – Alasdair