'list' object has no attribute 'queryset' error when adding a autocomplete field to a model-form
Asked Answered
K

2

1

I want to add an another autocomplete field to my model.form.

However, as soon as I add the autocomplete widget for the field "projektnummer"

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

I get an wired template error I cannot make sense of. Do you have some ideas what I can do here?

Thank you very much! :)

error message:

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
            )

edit:

I get the very same error message with a reduced test form without crispy forms. Just the plain model-form and the one autocomplete widget. So it seems to be unreleated to crispy forms, but related to the autocomplete widget.

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

template.html

{{ form }}

error:

 Exception Value: 'list' object has no attribute 'queryset'
Katzenjammer answered 14/10, 2016 at 16:59 Comment(2)
Can you paste the full traceback ?Terricolous
sure - i posted it as an extra answer, since it is too long. thank you for looking into it! :)Katzenjammer
S
2

You haven't posted your models but I am guessing that the problem might be that the field you are trying to autocomplete is not a ForeignKey or ManyToMany field but something like a CharField or IntegerField.

If you want to use it like this, you can do what Alex suggested. Otherwise you need to change your models.

Sensitivity answered 14/3, 2018 at 10:44 Comment(0)
P
1

I had same issue when applying to django-taggit. I changed form's widgets to ListSelect2 or TagSelect2. And then it works.

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

You can find more resources from 'Select2 widget implementation module.' : http://django-autocomplete-light.readthedocs.io/en/master/api.html#module-dal.widgets

Pidgin answered 27/12, 2016 at 3:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.