How To Automatically Register All Models In Django Admin? The inbuilt 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. admin interface 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 admin.py file and add this code to it. django.apps apps models = apps.get_models() model models: admin.site.register(model) from import for in 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 admin.py file and it should ignore models which are already registered. We can add this code at the end of the admin.py file. django.apps apps book.models Book = ( , ) # model registered custom admin admin.site.register(Book, BookAdmin) # all other models models = apps.get_models() model models: : admin.site.register(model) except admin.sites.AlreadyRegistered: pass from import from import ( . ): class BookAdmin admin ModelAdmin list_display 'name' 'author' with for in try Now manually registered models will get registered first followed by automatic registration of remaining models. = models.CharField(max_length= ) author = models.ForeignKey(Author, =True) borrowed = models.CharField(max_length= , = ) def __str__(self): self.name ( . ): class Book models Model name 100 null 100 default '' return 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. ListAdminMixin list_display 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. ListAdminMixin ModelAdmin django.apps apps django.contrib admin = [field.name field model._meta.fields] (ListAdminMixin, self).__init__(model, admin_site) models = apps.get_models() model models: admin_class = type( , (ListAdminMixin, admin.ModelAdmin), {}) : admin.site.register(model, admin_class) except admin.sites.AlreadyRegistered: pass from import from import ( ): ( , , ): . class ListAdminMixin object def __init__ self model admin_site self list_display for in super for in 'AdminClass' try 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 https://avilpage.com on November 24, 2017. More Django Tips & Tricks at https://avilpage.com/tags/django.html