How to get Django-Ajax-Selects to work in Django Admin?

天涯浪子 提交于 2019-12-11 02:55:31

问题


Django Ajax Selects

Here's what I did, to no avail:

Added ajax_select to my INSTALLED_APPS in settings.py

Added (r'ajax_select', include('ajax_select.urls')), to urls.py

Added this to settings.py:

AJAX_LOOKUP_CHANNELS = {
    'postal_code': {'model': 'places.PostalCode', 'search_field': 'code'}
}

Added this to admin.py:

class AddressAdmin(admin.ModelAdmin):
    form = make_ajax_form(Address, {'postal_code':'postal_code'})

admin.site.register(Address, AddressAdmin)

When I tried viewing the relevant page in the admin site and typed in the text box nothing came up. Looking in Firebug, nothing seems to be happening. Presumably because no JS is included; I guess the form/widgets don't include any the way the other admin widgets do.

So, I tried including the media the only way I know how:

class AddressForm(forms.ModelForm):
    postal_code = AutoCompleteSelectField('postal_code')

    class Media:
        css = {
            'all': ['media/css/ui-lightness/jquery-ui-1.8.9.custom.css', 'media/css/iconic.css']
        }
        js = ['media/js/jquery-1.4.4.min.js', 'media/js/jquery-ui-1.8.9.custom.min.js', 'media/js/ajax_select.js']

class AddressAdmin(admin.ModelAdmin):
    form = AddressForm

admin.site.register(Address, AddressAdmin)

Now everything gets included, but it still doesn't work. Nothing seems to be happening.

Did I miss a step? Why aren't any JS events firing?


回答1:


Okay, the problem seems to be that I have to use the old version of autocomplete.



来源:https://stackoverflow.com/questions/4872545/how-to-get-django-ajax-selects-to-work-in-django-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!