- August 22, 2008
- django models admin
- -1 (after 1 ratings)
Tired of adding admin classes to admin.py whenever you add a model? This admin.py automatically keeps itself up-to-date with your models.py file.
It assumes if you have a model: MyModel, you want an admin class called AdminMyModel.
Regards, Luke Miller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
""" admin.py Auto-register admin classes based on models in your project Questions? email: [email protected] """ from django.contrib import admin from django.db import models as dmodels from myproject import models #get the models from myproject.models] mods = [x for x in models.__dict__.values() if issubclass(type(x), dmodels.base.ModelBase)] admins =  #for each model in our models module, prepare an admin class #that will edit our model (Admin<model_name>, model) for c in mods: admins.append(("%sAdmin"%c.__name__, c)) #create the admin class and register it for (ac, c) in admins: try: #pass gracefully on duplicate registration errors admin.site.register(c, type(ac, (admin.ModelAdmin,), dict())) except: pass