管理员的django自动完成灯:它是如何工作的?

时间:2015-04-14 16:38:55

标签: python django autocomplete django-admin django-autocomplete-light

我正在尝试在管理员模型上制作可搜索的客户权限。

我已经安装了Django自动完成灯,但我看不到作品。

我正在尝试这个。

admin.py

import autocomplete_light

class RedAdminForm(forms.ModelForm):
    class Meta:
        model = Red
        widget = {'customer': autocomplete_light.ChoiceWidget('CustomerAutocomplete')}


class RedAdmin(admin.ModelAdmin):
    #form = RedAdminForm
    list_display = ("id", "customer","sponsor")
    list_per_page = 10
    search_fields=['customer__username', 'sponsor__username']
    #raw_id_fields = ('customer','sponsor',)
    form = autocomplete_light.modelform_factory(Red)

admin.site.register(Red, RedAdmin)

我还拥有模块的所有css和js静态文件,已经安装并安装了已安装的应用程序。

autocomplete_light_registry.py

import autocomplete_light
from django.contrib.auth.models import User

autocomplete_light.register(User)

在ajax请求中显示错误:

FieldError at /autocomplete/UserAutocomplete/ Cannot resolve keyword u'name' into field. Choices are: 

1 个答案:

答案 0 :(得分:0)

目前,autocomplete-light并未重复使用ModelAdmin的search_fields。有一个open PR for that

尝试使用search_fields注册,即:

registry.register(User, search_fields=['username', 'email'])