Last active
December 10, 2016 15:53
-
-
Save emakarov/7629a562a93064fcb86cb7661f45e4d2 to your computer and use it in GitHub Desktop.
Automatic django admin classes generation snippet
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.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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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