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! >_<
Project:
- settings.py
- admin.py (This is what I am trying to get to work)
- ...
- App
- admin.py (I know how to do this)
For example, admin.autodiscover()
is typically put in the project level urls.py
(yes, it will be automatically included in 1.7)
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 UserUserAdmin.list_display = ('email', 'first_name', 'last_name', 'is_active')
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
admin.autodiscover()
I tried doing just that, making an admin.py
file and adding the code into it. Didn't work.
I tried adding a project level folder called admin
, added an __init__.py
and threw admin.py
into the admin folder. Didn't work
Further I tried adding this admin
folder to INSTALLED_APPS
in settings.py
. No luck.
From the admin.autodiscover
doc string:
Auto-discover INSTALLED_APPS admin.py modules and fail silently when not present. This forces an import on them to register any admin bits theymay want.
And here is the complete function very well documented:
def autodiscover():"""Auto-discover INSTALLED_APPS admin.py modules and fail silently whennot present. This forces an import on them to register any admin bits theymay want."""import copyfrom django.conf import settingsfrom django.utils.importlib import import_modulefrom django.utils.module_loading import module_has_submodulefor app in settings.INSTALLED_APPS:mod = import_module(app)# Attempt to import the app's admin module.try:before_import_registry = copy.copy(site._registry)import_module('%s.admin' % app)except:# Reset the model registry to the state before the last import as# this import will have to reoccur on the next request and this# could raise NotRegistered and AlreadyRegistered exceptions# (see #8245).site._registry = before_import_registry# Decide whether to bubble up this error. If the app just# doesn't have an admin module, we can ignore the error# attempting to import it, otherwise we want it to bubble up.if module_has_submodule(mod, 'admin'):raise
So the only thing autodiscover does for you is look for some module called admin.py
inside installed app directories, hence you can put your admin.py where you want just make sure you import it, this make the code in it (registration of models ect..) get executed.
IMPORTANT: I'm not sure the correct moment for importing your custom-path admin.py
. But it's sure you have to import it after load all the related apps.