Created
August 3, 2012 17:56
-
-
Save scovetta/3249996 to your computer and use it in GitHub Desktop.
Django Automatic Model Registration for Django Admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
And within the try: part:
if not model._meta.abstract:
admin.site.register(model)
to prevent registering abstract models.
Thanks for the gist!
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
add
from django.contrib import admin