Skip to content

Instantly share code, notes, and snippets.

@sandiprb
Last active June 6, 2020 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandiprb/4ad0ae63c9a590f36d68f0456b94a942 to your computer and use it in GitHub Desktop.
Save sandiprb/4ad0ae63c9a590f36d68f0456b94a942 to your computer and use it in GitHub Desktop.
Django admin register all models of an app dynamically
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