0

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)
Community
  • 1
  • 1

1 Answers1

1

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.

jpic
  • 32,891
  • 5
  • 112
  • 113
  • That's it, thank you! Now I'm having problems trying to filter the tags based on the request user, without getting a "cannot filter a query once a slice has been taken"? I'm trying something like: class TagAutocomplete(autocomplete_light.AutocompleteModelBase): def choices_for_request(self): choices = super(TagAutocomplete, self).choices_for_request() return choices.filter(cartao__user=self.request.user) –  Oct 10 '13 at 20:44
  • Could you try to format your new problem in a new question please ? Thanks – jpic Oct 11 '13 at 11:08
  • sure, there you go: http://stackoverflow.com/questions/19319682/cant-make-autocomplete-light-filter-taggit-tags-based-on-request-user –  Oct 11 '13 at 13:42
  • @jpic Where are `TagField` and `TagWidget` defined here? – sebastian Jan 11 '14 at 19:51