QuerySet 对象没有属性标签 Django

QuerySet object has no attribute label Django

您好,我正在尝试使用从我的视图发送的一些额外数据来渲染模型表单。但是当我尝试使用 form.as_p:

访问表单时出现以下错误

QuerySet' object has no attribute 'label'

test.html

<form action="." method="post">
    {% csrf_token %}

    {{ form.as_p }}
 <div class="row">
  <div class="col-xs-5">
    {{form.tags}}

view.py

class ScheduledTestView(FormView):

serializer_class = TestShortSerializer

template_name = 'admin/scheduled_test.html'
form_class = ScheduledTestForm
initial = {'tags': Tag.objects.all()}


def form_valid(self, form):
    #some logic here

form.py

class ScheduledTestForm(forms.ModelForm):


    tags = forms.MultipleChoiceField(label='Tags', required=False)

    def __init__(self, *args, **kwargs):
        self.tags = kwargs['initial'].pop('tags', None)
        super(ScheduledTestForm, self).__init__(*args, **kwargs)
        self.fields['tags'] = self.tags

    class Meta:
        model = Test

错误回溯

Exception Type: AttributeError
Exception Value:    
     'QuerySet' object has no attribute 'label'
Exception Location:                   /home/kishan/.virtualenvs/kishan_pal/local/lib/python3.4/site- packages/django/forms/forms.py in __init__, line 526
 Python Executable: /home/kishan/.virtualenvs/kishan_pal/bin/python
 Python Version:    3.4.0

您应该将字段的 choices 属性设置为标签,您当前正在做的是将字段的 valueMultipleChoiceFieldQuerySet

所以如下:

self.fields['tags']= self.tags

应该是:

self.fields['tags'].choices = self.tags