Django自动完成多重选择给我无效的形式

时间:2017-02-02 13:32:17

标签: django django-autocomplete-light

我正在使用django-autocomplete-light而我正试图制作这样的形式:

from dal import autocomplete

class ReportForm(forms.Form):
    week_number = forms.MultipleChoiceField(
        choices=generate_week_numbers(), label=_("Week number"),
        widget=forms.SelectMultiple(),
        required=False
    )

    units = forms.MultipleChoiceField(
        widget=autocomplete.Select2Multiple(url='units-ac'), required=False
    )

自动填充代码:

class UnitsAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
    if not self.request.user.is_authenticated():
        return Unit.objects.none()
    Unit.objects.filter(#somefilters#)

    if self.q:
        qs = qs.filter(name__icontains=self.q)

    return qs

当我对表单进行汇总时,我收到错误:

<ul class="errorlist"><li>units<ul class="errorlist"><li>Select a valid choice. 24 is not one of the available choices.</li></ul></li></ul>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您是否按照官方文档中的示例进行操作? http://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#create-an-autocomplete-view 您错过了week_number字段中的queryset属性。