Django-admin autoregister -- automatic model registration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
""" Django-admin autoregister -- automatic model registration

## sample admin.py ##

from yourproject.autoregister import autoregister

# register all models defined on each app
autoregister('app1', 'app2', 'app3', ...)

"""

from django.db.models import get_models, get_app
from django.contrib import admin
from django.contrib.admin.sites import AlreadyRegistered

def autoregister(*app_list):
    for app_name in app_list:
        app_models = get_app(app_name)
        for model in get_models(app_models):
            try:
                admin.site.register(model)
            except AlreadyRegistered:
                pass

More like this

  1. Autogenerate admin classes in admin.py by dodgyville 4 years, 9 months ago
  2. Automatically generate admin by WoLpH 5 months, 2 weeks ago
  3. easy admin registration by alia_khouri 4 years, 8 months ago
  4. update-django: Update your django git branches. by telenieko 4 years, 6 months ago
  5. Django Registration without username by newmaniese 5 years, 1 month ago

Comments

JMagnusson (on July 21, 2010):

Nice one. Is there any way one could register ModelAdmin objects with this snippet?

#

JMagnusson (on July 21, 2010):

Update:

Broke the Sites framework for me. With this snippet in use it always shows the first site's admin page.

#

(Forgotten your password?)