Login

Django-admin autoregister -- automatic model registration

Author:
Samus_
Posted:
June 16, 2010
Language:
Python
Version:
1.1
Tags:
django models admin model django-admin autoregister auto-register
Score:
3 (after 3 ratings)

Automatically register models for the Django-admin.

This snippet aids in the process of keeping your admin.py up-to-date with the models in your apps, have all models automatically added to the admin and reflect any future changes without updating the file.

zweckdev.com

 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. Django admin autoregister by Mimino 1 year, 4 months ago
  2. easy admin registration by alia_khouri 6 years, 6 months ago
  3. Autogenerate admin classes in admin.py by dodgyville 6 years, 7 months ago
  4. Create permissions for proxy models by charettes 3 years, 1 month ago
  5. Automatically generate admin by WoLpH 2 years, 3 months ago

Comments

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.

#

Mimino (on November 8, 2013):

You can try: https://github.com/Mimino666/django-admin-autoregister It just works!

#

Please login first before commenting.