Created
September 4, 2017 14:30
-
-
Save jfsanchez91/5af7b9cfd9f5d451128591f66735f771 to your computer and use it in GitHub Desktop.
Django admin automatic model registration method.
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
""" 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.apps import apps | |
from django.contrib import admin | |
from django.contrib.admin.sites import AlreadyRegistered | |
def admin_autoregister_models(*app_list): | |
for app_name in app_list: | |
app_config = apps.get_app_config(app_name) | |
app_models = app_config.get_models(False, False) | |
for model in app_models: | |
try: | |
admin.site.register(model) | |
except AlreadyRegistered: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment