Question

I'm having a hard time integrating them together, when accessing '/autocomplete/CartaoAutocomplete/' I get "Related Field has invalid lookup: icontains". Relevant code:

models.py

class Cartao(models.Model):
    ...
    tags = TaggableManager()

autocomplete_light_registry.py

...
autocomplete_light.register(Cartao,
    search_fields=['tags'],
)

forms.py

...
class CartaoForm(ModelForm):
    tags = TagField(widget=TagWidget('CartaoAutocomplete'))

admin.py

...
class CartaoAdmin(admin.ModelAdmin):
    form = autocomplete_light.modelform_factory(Cartao)
admin.site.register(Cartao, CartaoAdmin)
Était-ce utile?

La solution

The following registers an Autocomplete for "Cartao", which means that it will suggest "Cartao" objects. And what you are trying to do is an Autocomplete to suggest tags, which are Tag instances. Instead of this:

# autocomplete_light_registry.py
autocomplete_light.register(Cartao,
    search_fields=['tags'],
)

# forms.py
class CartaoForm(ModelForm):
    tags = TagField(widget=TagWidget('CartaoAutocomplete'))

You should have:

# autocomplete_light_registry.py
from taggit.models import Tag
autocomplete_light.register(Tag)

# forms.py
class CartaoForm(ModelForm):
    tags = TagField(widget=TagWidget('TagAutocomplete'))

Let me know if this is correct then I will update the documentation.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top