admin.py用于项目,而不是应用程序 [英] admin.py for project, not app
问题描述
如何指定项目级别admin.py?
How can I specify a project level admin.py?
我前段时间问了这个问题,由于缺乏活动,我刚刚被授予了风滚草奖!> _<
I asked this question some time ago and was just awarded the Tumbleweed award because of the lack of activity on the question! >_<
项目:
- settings.py
- admin.py(这就是我要开始工作的目标)
- ...
- 应用
- admin.py(我知道该怎么做)
例如,通常将
admin.autodiscover()
放在项目级别的urls.py
中(是的,它将自动包含在1.7中)For example,
admin.autodiscover()
is typically put in the project levelurls.py
(yes, it will be automatically included in 1.7)我想将此文件及以下文件移动到自己的
admin.py
文件中:I would like to move this, and the following, into their own
admin.py
file:from django.contrib import admin from django.contrib.auth.admin import UserAdmin from django.contrib.auth.models import User UserAdmin.list_display = ('email', 'first_name', 'last_name', 'is_active') admin.site.unregister(User) admin.site.register(User, UserAdmin) admin.autodiscover()
我尝试这样做,制作一个
admin.py
文件并将代码添加到其中.没用.I tried doing just that, making an
admin.py
file and adding the code into it. Didn't work.我尝试添加一个名为
admin
的项目级别文件夹,添加了__ init __.py
并将admin.py
放入admin文件夹.没用I tried adding a project level folder called
admin
, added an__init__.py
and threwadmin.py
into the admin folder. Didn't work我还尝试将此
admin
文件夹添加到settings.py
中的INSTALLED_APPS
中.没有运气.Further I tried adding this
admin
folder toINSTALLED_APPS
insettings.py
. No luck.推荐答案
迟早要进行大量定制的
admin
类很可能需要整个项目范围的admin.py
,因为它是注册不再自动发现的所有按应用程序管理员的地方.文档中提到了这一点,但是没有广泛的示例来说明这一点:Sooner or later, a heavily customized
admin
class will most likely need a project wideadmin.py
, as it is the place to register all per-app admins that are not being auto-discovered anymore. The documentation mentions this, but there is no extensive example to illustrate it:https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#customizing-the-adminsite-class
请注意,在使用自己的
AdminSite
实例时,您可能不希望自动发现admin
模块,因为您很可能会导入所有每个应用程序的admin 您的
myproject.admin
模块中的code>模块.这意味着您需要在INSTALLED_APPS
中放置'django.contrib.admin.apps.SimpleAdminConfig'
而不是'django.contrib.admin'
设置.Note that you may not want autodiscovery of
admin
modules when using your ownAdminSite
instance since you will likely be importing all the per-appadmin
modules in yourmyproject.admin
module. This means you need to put'django.contrib.admin.apps.SimpleAdminConfig'
instead of'django.contrib.admin'
in yourINSTALLED_APPS
setting.对于多个
admin
网站,Wiki中还有一个已经过时但或多或少有效的示例:There is a bit outdated but more or less valid example in the wiki as well for multiple
admin
sites:https://code.djangoproject.com/wiki/NewformsAdminBranch
# project-level admin.py from django.contrib import admin from myproject.myapp.models import Book, Author from myproject.anotherapp.models import Musician, Instrument site1 = admin.AdminSite() site1.register(Book) site1.register(Author) site2 = admin.AdminSite() site2.register(Musician) site2.register(Instrument)
这篇关于admin.py用于项目,而不是应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!