Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

admin.py for project, not app

Tags:

python

django

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 User

UserAdmin.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.

like image 340
Douglas Denhartog Avatar asked Jun 09 '14 20:06

Douglas Denhartog


People also ask

What is Admin py used for?

The admin.py file is used to display your models in the Django admin panel. You can also customize your admin panel. Hope this helps you.

Can I use Django admin in production?

your company should follow a least access principle policy; so yes: only select people should have access. Django has basic auditing capability via signals and displayed in the admin out of the box. You can build on top of this.

What is the purpose of the admin site in a Django project?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.

What is the admin file in Django?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.


1 Answers

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 they may want.

And here is the complete function very well documented:

def autodiscover():
    """
    Auto-discover INSTALLED_APPS admin.py modules and fail silently when
    not present. This forces an import on them to register any admin bits they
    may want.
    """

    import copy
    from django.conf import settings
    from django.utils.importlib import import_module
    from django.utils.module_loading import module_has_submodule

    for 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.

like image 68
Raydel Miranda Avatar answered Sep 18 '22 04:09

Raydel Miranda