chillar anand

Automatically Register All Models In Django Admin — Django Tips

How To Automatically Register All Models In Django Admin?
The inbuilt admin interface is one of the most powerful & popular features of Django. Once we create the models, we need to register them with the admin interface, so that it can read metadata and populate interface for it.
If the Django project has too many models or if it has a legacy database, then adding all those models to admin becomes a tedious task. To automate this process, we can programmatically fetch all the models in the project and register them with the admin interface.
Open file and add this code to it.
from django.apps import apps

models = apps.get_models()

for model in models:
This will fetch all the models in all apps and registers them with the admin interface.
This works well if we are automatically registering all the models. However, if we separately register some models with customizations and try to register all models in our apps again, there will be conflicts as Django doesn't allow registering the same model twice.
So, we need to make sure this piece of code runs at the end of file and it should ignore models which are already registered. We can add this code at the end of the file.
from django.apps import apps
from book.models import Book
class BookAdmin(admin.ModelAdmin):
    list_display = ('name', 'author')
# model registered with custom admin, BookAdmin)
# all other models
models = apps.get_models()

for model in models:
    except admin.sites.AlreadyRegistered:
Now manually registered models will get registered first followed by automatic registration of remaining models.
class Book(models.Model):
    name = models.CharField(max_length=100)
    author = models.ForeignKey(Author, null=True)
    borrowed = models.CharField(max_length=100, default='')

    def __str__(self):
If we go to a model page in admin, automatically registered models will just show 1 column like this.
This interface is not informative for the users who want to see the data. To improve that, we can create a
, which will populate
with all the fields in the model.
We can create a new admin class which will subclass
. We can use this admin class when we are registering the model so that all the fields in the model will show up in the admin.
from django.apps import apps
from django.contrib import admin

class ListAdminMixin(object):
    def __init__(self, model, admin_site):
        self.list_display = [ for field in model._meta.fields]
        super(ListAdminMixin, self).__init__(model, admin_site)

models = apps.get_models()
for model in models:
    admin_class = type('AdminClass', (ListAdminMixin, admin.ModelAdmin), {})
    try:, admin_class)
    except admin.sites.AlreadyRegistered:
Now, whenever we create a new model or add a new field to an existing model, it will get reflected in the admin automatically.
With this, we can avoid registering models with admin whenever models get added to our apps.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — -
Originally published at on November 24, 2017.
More Django Tips & Tricks at


More by chillar anand

Topics of interest