我的ModelMultipleChoiceField
中的form.py
格式如下:
class UserResponseSearchForm(forms.Form):
def __init__(self, *args, **kwargs):
qry = kwargs.pop('qry')
super(UserResponseSearchForm,self).__init__(*args, **kwargs)
self.fields['gejala_id0'] = forms.ModelMultipleChoiceField(queryset=Gejala.objects.filter(gejala__icontains=qry).values_list('gejala', flat=True).distinct().order_by('gejala'),widget=forms.CheckboxSelectMultiple, required=False)
gejala_id1 = forms.ModelMultipleChoiceField(queryset=Gejala.objects.all().values_list('gejala', flat=True).distinct().filter(id_organ=1).order_by('gejala'), widget=forms.CheckboxSelectMultiple, required=False)
gejala_id2 = forms.ModelMultipleChoiceField(queryset=Gejala.objects.all().values_list('gejala', flat=True).distinct().filter(id_organ=2).order_by('gejala'), widget=forms.CheckboxSelectMultiple, required=False)
gejala_id3 = forms.ModelMultipleChoiceField(queryset=Gejala.objects.all().values_list('gejala', flat=True).distinct().filter(id_organ=3).order_by('gejala'), widget=forms.CheckboxSelectMultiple, required=False)
我的views.py是这样的:
def responsePenyakit(request):
if request.user.is_authenticated:
if request.method == 'POST':
form = UserResponseForm(request.POST)
gejala0 = form["gejala_id0"].data
gejala1 = form["gejala_id1"].data
gejala2 = form["gejala_id2"].data
gejala3 = form["gejala_id3"].data
if (len(gejala0) > 0):
for i in range(0, len(gejala0)):
userAnswer = UserAnswer()
userAnswer.gejala_answer = gejala1[i]
userAnswer.user_id = request.user.id
userAnswer.number_diagnosis = user.number_diagnosis + 1
userAnswer.save()
print(gejala0)
if (len(gejala1) > 0):
for i in range(0, len(gejala1)):
userAnswer = UserAnswer()
userAnswer.gejala_answer = gejala1[i]
userAnswer.user_id = request.user.id
userAnswer.number_diagnosis = user.number_diagnosis + 1
userAnswer.save()
if (len(gejala2) > 0):
for i in range(0, len(gejala2)):
userAnswer = UserAnswer()
userAnswer.gejala_answer = gejala2[i]
userAnswer.user_id = request.user.id
userAnswer.number_diagnosis = user.number_diagnosis + 1
userAnswer.save()
if (len(gejala3) > 0):
for i in range(0, len(gejala3)):
userAnswer = UserAnswer()
userAnswer.gejala_answer = gejala3[i]
userAnswer.user_id = request.user.id
userAnswer.number_diagnosis = user.number_diagnosis + 1
userAnswer.save()
return redirect('diagnosis_penyakit:response_matching')
else:
raise Http404
我想获取从gejala_id0
表格中选择的数据。我尝试像在views.py
中那样获取数据。
但是有返回错误Key 'gejala_id0' not found in 'UserResponseForm'.
,这是什么问题?我认为我所做的是正确的。 CMIIW
请帮助我。我是django的新人
答案 0 :(得分:0)
首先创建字段,然后在__init__
中更改查询集。
class UserResponseSearchForm(forms.Form):
def __init__(self, *args, **kwargs):
qry = kwargs.pop('qry')
super(UserResponseSearchForm,self).__init__(*args, **kwargs)
self.fields['gejala_id0'].queryset = Gejala.objects.filter(gejala__icontains=qry).values_list('gejala', flat=True).distinct().order_by('gejala')
gejala_id0 = forms.ModelMultipleChoiceField(queryset=Gejala.objects.none(), widget=forms.CheckboxSelectMultiple, required=False)
gejala_id1 = forms.ModelMultipleChoiceField(queryset=Gejala.objects.all().values_list('gejala', flat=True).distinct().filter(id_organ=1).order_by('gejala'), widget=forms.CheckboxSelectMultiple, required=False)
gejala_id2 = forms.ModelMultipleChoiceField(queryset=Gejala.objects.all().values_list('gejala', flat=True).distinct().filter(id_organ=2).order_by('gejala'), widget=forms.CheckboxSelectMultiple, required=False)
gejala_id3 = forms.ModelMultipleChoiceField(queryset=Gejala.objects.all().values_list('gejala', flat=True).distinct().filter(id_organ=3).order_by('gejala'), widget=forms.CheckboxSelectMultiple, required=False)
还将qry发送给表单:
def responsePenyakit(request):
if request.user.is_authenticated:
if request.method == 'POST':
form = UserResponseForm(data=request.POST, qry=query)