我想做的事情似乎很简单:我有一个django形式的MultipleChoiceField,它提出了名为Dico的模型中所有书籍的id和伪:
class FiltreMonoForm(forms.Form):
dico_choices = []
for dic in Dico.objects.all().order_by('pseudo'):
dico_choices.append((dic.id, dic.pseudo))
dico_choices=tuple(dico_choices)
dicos = forms.MultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple, choices=dico_choices, initial=[c[0] for c in dico_choices], label="Diccionaris sorsas")
我在一个视图中找到了这个表格:
def monollist(request):
if request.GET:
getcopy = request.GET.copy()
form = FiltreMonoForm(getcopy)
dicos = form.cleaned_data['dicos']
else:
form = FiltreMonoForm()
如果用户没有选中任何书籍,我想检查所有书籍。类似的东西:
if request.GET:
getcopy = request.GET.copy()
form = FiltreMonoForm(getcopy)
dicos = form.cleaned_data['dicos']
for dic in Dico.objects.all().order_by('pseudo'):
dico_choices.append((dic.id, dic.pseudo))
dico_choices=tuple(dico_choices)
if len(dicos)==0:
form['dicos']=dico_choices
但是我找不到如何更改MultipleChoicesField的值。我试过
form.data['dicos']=dico_choices
但似乎我只能给form.data ['dicos']一个值,它不会接受列表也不接受元组。
我试图在添加
的表单中覆盖__init__方法self.data.update(dicos=dico_choices)
但是我有同样的问题,只有当dico_choices是单个值时它才有效。
您是否知道如何使用多个值覆盖我的MultipleChoiceField?
由于
答案 0 :(得分:0)
En attendant und une un plus plus plus plus plus plus plus plus plus plus plus plus plus plus ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
if 'dicos' not in getcopy:
for dic in Dico.objects.all():
getcopy.update({'dicos': dic.id})