Search code examples
djangodjango-admin

How to register multiple models with the admin?


If I want to register my models with the admin I have to do this like this:

# admin.py
admin.site.register(models.About)

But with multiple models you can't do something like this:

models = (models.Project, models.Client, models.About)
for m in models:
   admin.site.register(m)

First of all, why not?

Secondly, imagine one has a lot of models which all should be accessible from the admin interface. How do you do that in a generic way?


Solution

  • admin.site.register has this definition in the library:

    def register(self, model_or_iterable, admin_class=None, **options):
    

    so models to be registered can be a single model or iterable object so just use this:

    myModels = [models.Project, models.Client, models.About]  # iterable list
    admin.site.register(myModels)
    

    I tested this in my site and works perfectly fine.