原创声明:本文除了标明引用的内容外,都为本人原创,请尊重本人的知识产权,不能用于商业用途。欢迎转载,转载请在文章开头处插入以下内容:
本文转载自"狂龙ing”的Blog,地址为“http://blog.csdn.net/kuanglong2016/article/details/17173939"
>>> pip install django-extensions
>>> python -c "import django_extensions; print django_extensions.VERSION"
INSTALLED_APPS = (
...
'django_extensions',
)
from django.contrib import admin
from django_extensions.admin import ForeignKeyAutocompleteAdmin
import models
# Register your models here.
class LessonAdmin(ForeignKeyAutocompleteAdmin):
related_search_fields = {
'xyClass': ('name',)
}
admin.site.register(models.XYClass)
admin.site.register(models.Lesson, LessonAdmin)
4. 完成后测试可以正常访问/admin/,但是页面还是没有任何变化,select也没变,通过Firebug查看可以看到错误如下
from django.contrib import admin
from django_extensions.admin import ForeignKeyAutocompleteAdmin
import models
# Register your models here.
class LessonAdmin(ForeignKeyAutocompleteAdmin):
related_search_fields = {
'xyClass': ('name',)
}
class Media:
js = ("//code.jquery.com/jquery-1.10.2.min.js",) #在这里添加自己的jQuery库
admin.site.register(models.XYClass)
admin.site.register(models.Lesson, LessonAdmin)
使用Firebug测试,$已经可以使用,但是还是出现下面错误:
>>> pip install django-autocomplete-light
INSTALLED_APPS = [
...
'autocomplete_light',
]
3. 在urls.py里的
admin.autodiscover()
前添加
autocomplete_light.autodiscover()
添加代码后的样子一般如下
import autocomplete_light
# import every app/autocomplete_light_registry.py
autocomplete_light.autodiscover()
import admin
admin.autodiscover()
4. 添加autocomplete_light.urls:在使用自动完成的时候,admin会使用ajax的方式与autocomplete_light.urls通信
from django.conf.urls import patterns, url, include
urlpatterns = patterns('',
...
url(r'^autocomplete/', include('autocomplete_light.urls')),
)
5. 修改admin/base_site.html,该文件一般都在/usr/local/lib/python[版本号]/dist-packages/django/contrib/admin/templates/admin里。先备份base_site.html,再用以下内容覆盖
{% extends "admin/base.html" %}
{% block extrahead %}
{% include 'autocomplete_light/static.html' %}
{% endblock %}
6. 注册Autocomplete类:添加app/autocomplete_light_register.py,内容一般如下
import autocomplete_light
from models import Person
# This will generate a PersonAutocomplete class
autocomplete_light.register(Person,
# Just like in ModelAdmin.search_fields
search_fields=['^first_name', 'last_name'],
# This will actually html attribute data-placeholder which will set
# javascript attribute widget.autocomplete.placeholder.
autocomplete_js_attributes={'placeholder': 'Other model name ?',},
)
7. 为admin提供具有自动完成功能的ModelForms
from django.contrib import admin
import autocomplete_light
from models import Order
class OrderAdmin(admin.ModelAdmin):
# This will generate a ModelForm
form = autocomplete_light.modelform_factory(Order)
admin.site.register(Order)
按照指南中的说法,到这里应该已经可以实现外键的自动完成功能了。于是我便满怀期待地使用Admin去添加Order记录,但是还是没有任何变化,打开Firebug也没有看到任何错误。郁闷...真的出了郁闷还是郁闷....郁闷...真的太郁闷了...6个小时居然啥也没干成,我都快要崩溃了。不过真是应了这句“山穷水复疑无路,柳暗花明又一村”,就在我快要放弃的时候,我突然发现我打开的一个页面里的内容与上面有一点点不一样
,大喜!!!(为什么还没有使就会大喜呢,嘿嘿嘿,说来惭愧,因为之前看到admin.site.register(Order)时就觉得纳闷,为什么不是Admin常用的admin.site.register(Order, OrderAdmin)呢,但是也没有深究,后来不成功也没有往这方面想。现在居然看到了我原来想的那种用法,当然很开心,感觉很可能问题就是它了)
from django.contrib import admin
import autocomplete_light
from models import Order
class OrderAdmin(admin.ModelAdmin):
# This will generate a ModelForm
form = autocomplete_light.modelform_factory(Order)
#admin.site.register(Order) #error
admin.site.register(Order, OrderAdmin)