django-autocomplete-light with User - 选择应该是一个查询集

时间:2014-09-29 22:01:16

标签: django django-autocomplete-light

我似乎无法使django-autocomplete-light与django contrib用户模型一起工作。总是得到例外'选择应该是一个查询集'

这是我的自动完成类(在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)

我的表单(在forms.py中):

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

尝试使用{{form}}进行渲染时,会抛出异常:选项应该是一个查询集: 堆栈:

/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' 

这是django 1.6在开发中运行。我创建了用户。 django-autocomplete-light可以使用autocompleteListBase工作,例如:

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

所以urls.py包含注册表,url已注册并正在加载javascript。 遵循这些文档: http://django-autocomplete-light.readthedocs.org/en/latest/index.html#tutorial

任何指针?

谢谢!

1 个答案:

答案 0 :(得分:1)

嗯,明白了......文档没有提及,但需要在自动完成类中明确定义选择。

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