Last active
August 29, 2015 14:19
-
-
Save Sunno/37d0b5987415627f66e9 to your computer and use it in GitHub Desktop.
Register all models from an app in 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
__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