Auto Register All Models In Admin

Manual Registration

Inbuilt admin interface is one the most powerful & popular feature of Django. Once we create the models, we need to register them with admin, so that it can read schema and populate interface for it.

Let us register Book model in the admin interface.

# file: library/book/admin.py

from django.apps import apps

from book.models import Book


class BookAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'author')


admin.site.register(Book, BookAdmin)

Now, we can see the book model in admin.

_images/admin-auto-register1.png

If the django project has too many models to be registered in admin or if it has a legacy database where all tables need to be registered in admin, then adding all those models to admin becomes a tedious task.

Auto Registration

To automate this process, we can programatically fetch all the models in the project and register them with admin. Also, we need to ignore models which are already registered with admin as django doesn’t allow regsitering same model twice.

from django.apps import apps


models = apps.get_models()

for model in models:
    try:
        admin.site.register(model)
    except admin.sites.AlreadyRegistered:
        pass

This code snippet should run after all admin.py files are loaded so that auto registration happends after all manually added models are registered. Django provides AppConfig.ready() to perform any initialization tasks which can be used to hook this code.

# file: library/book/apps.py

from django.apps import apps, AppConfig
from django.contrib import admin


class BookAppConfig(AppConfig):

    def ready(self):
        models = apps.get_models()
        for model in models:
            try:
                admin.site.register(model)
            except admin.sites.AlreadyRegistered:
                pass

In the admin, we can see manually registered models and automatically registered models. If we open admin page for any auto registered model, it will show something like this.

_images/admin-auto-register2.png

This view is not at all useful for the users who want to see the data. It will be more informative if we can show all the fields of the model in admin.

Auto Registration With Fields

To achieve that, we can create an admin class to populate model fields in list_display. While registering, we can use this admin class to register the model.

from django.apps import apps, AppConfig
from django.contrib import admin


class ListModelAdmin(admin.ModelAdmin):
    def __init__(self, model, admin_site):
        self.list_display = [field.name for field in model._meta.fields]
        super().__init__(model, admin_site)


class BookAppConfig(AppConfig):

    def ready(self):
        models = apps.get_models()
        for model in models:
            try:
                admin.site.register(model, ListModelAdmin)
            except admin.sites.AlreadyRegistered:
                pass

Now, if we look at Author admin page, it will be shown with all relevant fields.

_images/admin-auto-register3.png

Since we have auto registration in place, when a new model is added or columns are altered for existing models, admin interface will update accordingly without any code changes.