Last active
June 6, 2020 13:54
-
-
Save sandiprb/4ad0ae63c9a590f36d68f0456b94a942 to your computer and use it in GitHub Desktop.
Django admin register all models of an app dynamically
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
from django.contrib import admin | |
from django.apps import apps | |
from django.contrib.auth.admin import UserAdmin as DjUserAdmin | |
from . import models | |
# Register your models here. | |
@admin.register(models.User) | |
class UserAdmin(DjUserAdmin): | |
pass | |
# for explicit model registrations, register models above this | |
app_models = apps.get_app_config('core').get_models() # replace core with app name | |
for model in app_models: | |
try: | |
admin.site.register(model) | |
except Exception: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment