Tiago A.
Tiago A.

Reputation: 1482

django-autocomplete-light with User - choices should be a queryset

I can't seem to make django-autocomplete-light work with the django contrib user model. Always get exception 'choices should be a queryset'

This is my autocomplete class (defined in autocomplete_ligh_registry.py):

import autocomplete_light
from django.contrib.auth.models import User
class UserAutocomplete(autocomplete_light.AutocompleteModelBase):
    search_fields = ['first_name']
    model = User
autocomplete_light.register(UserAutocomplete)

my form (in forms.py):

class TransactionForm(forms.Form):
    mymodel = forms.ModelChoiceField(  required=True, 
                                       queryset=User.objects.all() , 
                                   widget=autocomplete_light.ChoiceWidget('UserAutocomplete'))

When trying to render with {{form}}, it throws an exception: choices should be a queryset: stack:

/home/prj/docs/projectos/.../src/autocomplete-light/autocomplete_light/widgets.py in render
        choices = autocomplete.choices_for_values() ...
▶ Local vars
/home/prj/docs/projectos/.../src/autocomplete-light/autocomplete_light/autocomplete/model.py in choices_for_values
        assert self.choices is not None, 'choices should be a queryset' 

This is django 1.6 running in development. I have Users created. django-autocomplete-light works ok with an autocompleteListBase, e.g.:

class OsAutocomplete(autocomplete_light.AutocompleteListBase):
    choices = ['Linux', 'BSD', 'Minix']
autocomplete_light.register(OsAutocomplete)

so urls.py are including the registry, urls are registered and javascript is being loaded. Following these docs: http://django-autocomplete-light.readthedocs.org/en/latest/index.html#tutorial

Any pointers?

Thanks!

Upvotes: 0

Views: 589

Answers (1)

Tiago A.
Tiago A.

Reputation: 1482

Hmm, got it... Docs don't mention but it needs choices to be explicitly defined on the autocomplete class.

class UserAutocomplete(autocomplete_light.AutocompleteModelBase):
    search_fields = ['email']
    choices = User.objects.all()
    model = User
autocomplete_light.register(UserAutocomplete)

Upvotes: 1

Related Questions