'列表'对象没有属性' queryset'将自动填充字段添加到模型表单

时间:2016-10-14 16:59:11

标签: django django-forms jquery-select2 django-crispy-forms django-autocomplete-light

我想在我的model.form中添加另一个自动填充字段。

但是,只要我为字段添加自动完成小部件" projektnummer"

'projektnummer': autocomplete.ModelSelect2(url='output:projekt-form-autocomplete'),

我收到了一个我无法理解的有线模板错误。你有什么想法我能在这做什么吗?

非常感谢! :)

错误消息:

AttributeError at /output/create/
'list' object has no attribute 'queryset'
Request Method: GET
Request URL:    http://127.0.0.1:8000/output/create/
Django Version: 1.8.7
Exception Type: AttributeError
Exception Value:    
'list' object has no attribute 'queryset'

Error during template rendering

In template /home/bjoern/Developement/Django/Outputmeldetool/venv_outputmeldetool/lib/python3.5/site-packages/crispy_forms/templates/bootstrap3/field.html, error at line 28

28 {% if field|is_checkbox and form_show_labels %}

forms.py

class KombiPublikationForm(forms.ModelForm):

    typtyp = forms.ModelChoiceField(label='Vorauswahl Outputtyp', required = False,
                                    queryset=KombiPublikationsTypTyp.objects.exclude(id__in=EXCLUDED_TYPTYP)
                                    )

    class Meta:
        model = KombiPublikation
        exclude = ['pub_sprache']
        widgets = {
            'freigabe': DateTimePicker(options={"format": "YYYY-MM-DD HH:mm", 'sideBySide': True}),
            'typid': autocomplete.ModelSelect2(url='output:typ-autocomplete', forward=['typtyp']),
#adding following line generates the error:
            'projektnummer': autocomplete.ModelSelect2(url='output:projekt-form-autocomplete'),
            'monat': forms.NumberInput(),
        }



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

        self.helper = FormHelper()
        self.helper.help_text_inline = True
        self.helper.form_tag = False
        self.helper.layout = Layout(
           #a lot of layout stuff is here
            )

编辑:

我得到了相同的错误消息,其中包含简化的测试表单,没有松散的表单。只是简单的模型形式和一个自动完成小部件。所以它似乎对脆弱的表单没有发挥作用,但与自动完成小部件有关。

class TestPublikationForm(forms.ModelForm):
    class Meta:
        model = KombiPublikation
        exclude = ['pub_sprache']
        widgets = {
            'projektnummer': autocomplete.ModelSelect2(url='output:projekt-form-autocomplete'),
    }

template.html

{{ form }}

错误:

 Exception Value: 'list' object has no attribute 'queryset'

2 个答案:

答案 0 :(得分:1)

您尚未发布模型,但我猜测问题可能是您尝试自动填充的字段不是ForeignKey或ManyToMany字段,而是类似CharField或IntegerField。

如果你想像这样使用它,你可以做Alex所建议的。否则你需要改变模型。

答案 1 :(得分:0)

申请django-taggit时遇到同样的问题。我将表单的小部件更改为ListSelect2或TagSelect2。然后它的工作原理。

class TestPublikationForm(forms.ModelForm):
class Meta:
    model = KombiPublikation
    exclude = ['pub_sprache']
    widgets = {
        'projektnummer': autocomplete.ListSelect2(url='output:projekt-form-autocomplete'),
}

您可以从 Select2小部件实施模块中找到更多资源。' : http://django-autocomplete-light.readthedocs.io/en/master/api.html#module-dal.widgets