Skip to content

Instantly share code, notes, and snippets.

@Sunno
Last active August 29, 2015 14:19
Show Gist options
  • Save Sunno/37d0b5987415627f66e9 to your computer and use it in GitHub Desktop.
Save Sunno/37d0b5987415627f66e9 to your computer and use it in GitHub Desktop.
Register all models from an app in django admin
__author__ = 'alvaro'
import inspect
from django.contrib import admin
import pyclbr
from django.db import models
def register_models(my_models):
"""
Call this from your admin.py in your app, this will register all your models
Example:
# my_app/admin.py
from my_app import models
from utils.admin import register_models
register_models(models)
:param my_models: your models module
"""
classes = pyclbr.readmodule_ex(my_models.__name__)
for model in classes:
model = getattr(my_models, model)
if inspect.isclass(model) and issubclass(model, models.Model):
admin.site.register(model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment