【问题标题】:Django Form refuses to render queryset in multiple select fieldDjango Form拒绝在多个选择字段中呈现查询集
【发布时间】:2013-07-01 15:07:53
【问题描述】:

我有以下 django 表单:

class AccountForm(Form):

    evalTypes = django_fields.MultipleChoiceField(label="Default Evaluation Forms", widget=django_widgets.SelectMultiple)

    def __init__(self, *args, **kwargs):        
        super(AccountForm, self).__init__(*args, **kwargs)

        # GET POSSIBLE LIST OF EVALUATIONS

        self.fields["evalTypes"].queryset = CustomForm.objects.filter(author__permissions__name__in=['manager'])

        for q in self.fields["evalTypes"].queryset:
            print q # THIS PRINTS ALL THE CORRECT VALUES

问题是当页面加载时,唯一出现的是空的选择框。这很奇怪,因为我有明确的证据证明查询集被正确填充。我可能会错过什么?

【问题讨论】:

    标签: python django forms field django-queryset


    【解决方案1】:

    MultipleChoiceField 接受 choices 而不是 queryset

    改用ModelMultipleChoiceField

    evalTypes = forms.ModelMultipleChoiceField(label="Default Evaluation Forms", widget=forms.SelectMultiple, queryset=None)
    

    【讨论】:

    • 谢谢,成功了。唯一的事情是您需要将查询集参数传递给 MultipleChoiceField。所以我做了: MultipleChoiceField(label="Default Evaluation Forms", widget=forms.SelectMultiple, queryset=None) 然后稍后添加查询集。
    • 是的。我错过了将那部分添加到答案中。
    猜你喜欢
    • 2023-04-09
    • 2011-10-26
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    相关资源
    最近更新 更多