Skip to content

Instantly share code, notes, and snippets.

@emakarov
Last active December 10, 2016 15:53
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 emakarov/7629a562a93064fcb86cb7661f45e4d2 to your computer and use it in GitHub Desktop.
Save emakarov/7629a562a93064fcb86cb7661f45e4d2 to your computer and use it in GitHub Desktop.
Automatic django admin classes generation snippet
from django.contrib import admin
from django.apps import apps
from django.contrib.admin.sites import AlreadyRegistered
def register_all_models(models):
for m in models:
m_admin_name = m.__module__.replace(".","") + m.__name__ + 'Admin'
ModelAdminClass = type(m_admin_name, (admin.ModelAdmin,), {'set_x': lambda x,y: x.y})
ModelAdminClass.model = m
try:
admin.site.register(m, ModelAdminClass)
except AlreadyRegistered:
pass
register_all_models(apps.get_models())
@emakarov
Copy link
Author

emakarov commented Dec 10, 2016

you can put it to any admin.py of django project (I suppose, django >= 1.7)
All models of all apps will be in admin interface with default settings.
Useful when working on rapid prototyping, need for admin and don't have time to write models manually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment