Skip to content

Instantly share code, notes, and snippets.

@scovetta
Created August 3, 2012 17:56
Show Gist options
  • Save scovetta/3249996 to your computer and use it in GitHub Desktop.
Save scovetta/3249996 to your computer and use it in GitHub Desktop.
Django Automatic Model Registration for Django Admin
"""
This gist allows you to automatically register all models in a given app
for use in the Django Admin application.
Change (your-app-name) to your application name.
"""
import inspect
from django.contrib.admin.sites import AlreadyRegistered
from (your-app-name) import models
model_list = [model_info[1] for model_info in inspect.getmembers(models)
if inspect.isclass(model_info[1])]
for model in model_list:
try:
admin.site.register(model)
except AlreadyRegistered:
pass
@nerdoc
Copy link

nerdoc commented Apr 26, 2018

And within the try: part:

            if not model._meta.abstract:
                admin.site.register(model)

to prevent registering abstract models.
Thanks for the gist!

@nerdoc
Copy link

nerdoc commented Apr 26, 2018

Oh, and better in Django 2.0 is my code, look at my fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment