문제

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)
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top