mogoh
mogoh

Reputation: 1073

Using django autocomplete light with form tools preview

I try to use django autocomplete light outside the admin panel in combination with form tools. I accomplished to use autocomplete inside the admin panel and to use regular forms in the django form tools. But the last step, to use autocomplete inside outside the admin panel will not work.

This is from my forms.py:

class PersonForm(forms.ModelForm):
    class Meta:
        model = Person
        fields = ('user',)
        widgets = {
            'user': autocomplete.ModelSelect2(url='autocomplete-person')
        }
[...]
class MessageForm(forms.Form):
    user = PersonForm()

I guess the error must be here, but I am not sure. What I think, what this should do: * Inherits from ModelForm * changes the widgets to the fitting aoutocomplete widget * Uses correct model and fields * In MessageForm this should be used.

Instead nothing shoes on the screen. Can someone help? I can provide other parts of my code if necessary.

Upvotes: 0

Views: 209

Answers (1)

Alasdair
Alasdair

Reputation: 309109

class MessageForm(forms.Form):
    user = PersonForm()

You can't instantiate one form inside another like this.

Instead, try using a ModelChoiceField for your user field, and set the widget there:

class MessageForm(forms.Form):
    user = forms.ModelChoiceField(queryset=Person.objects.all(), widget=autocomplete.ModelSelect2(url='autocomplete-person'))

Upvotes: 1

Related Questions