[Django]-Register every table/class from an app in the Django admin page

24πŸ‘

βœ…

I figured it out with @arie’s link (for django < 1.8):

from django.contrib import admin
from django.db.models import get_models, get_app

for model in get_models(get_app('doors')):
    admin.site.register(model)

But I wonder if I can do this without get_app… Couldn’t the code be smart enough to know the name of its own app?

πŸ‘€hobbes3

50πŸ‘

Seems get_models and get_app are no longer available in django 1.8.

The following can be used:

from django.contrib import admin
from django.apps import apps

app = apps.get_app_config('dashboard')

for model_name, model in app.models.items():
    admin.site.register(model)

EXTENSION: If you would like to show all or select fields of the model as a grid instead of a single column unicode representation of the model objects you may use this:

app = apps.get_app_config('your_app_name')
for model_name, model in app.models.items():
    model_admin = type(model_name + "Admin", (admin.ModelAdmin,), {})

    model_admin.list_display = model.admin_list_display if hasattr(model, 'admin_list_display') else tuple([field.name for field in model._meta.fields])
    model_admin.list_filter = model.admin_list_filter if hasattr(model, 'admin_list_filter') else model_admin.list_display
    model_admin.list_display_links = model.admin_list_display_links if hasattr(model, 'admin_list_display_links') else ()
    model_admin.list_editable = model.admin_list_editable if hasattr(model, 'admin_list_editable') else ()
    model_admin.search_fields = model.admin_search_fields if hasattr(model, 'admin_search_fields') else ()

    admin.site.register(model, model_admin)

What this does is, it extends ModelAdmin class on the fly and sets the list_display field which is required for showing model data in grid representation in the admin. If you list your desired fields in your model as admin_list_display it takes that one, or generates a tuple of all fields available in the model, otherwise.

Other optional fields can similarly be set, such as list_filter.

See django documentation for more info on list_display.

πŸ‘€mehmet

19πŸ‘

From Django 1.7 on, you can use this code in your admin.py:

from django.apps import apps
from django.contrib import admin
from django.contrib.admin.sites import AlreadyRegistered

app_models = apps.get_app_config('my_app').get_models()
for model in app_models:
    try:
        admin.site.register(model)
    except AlreadyRegistered:
        pass
πŸ‘€Sjoerd

8πŸ‘

From Django 1.8, to fix the error message

RemovedInDjango19Warning: django.db.models.get_app is deprecated.

We can use this approach in 2 lines

from django.contrib import admin
from my_app.models import *
from django.apps import apps

for model in apps.get_app_config('my_app').models.values():
    admin.site.register(model)
πŸ‘€Samuel Chan

1πŸ‘

from django.apps import apps
from django.contrib.admin.sites import AlreadyRegistered

 app_models = apps.get_app_config('app-name').get_models()
 for model in app_models:
     try:
         admin.site.register(model)
     except AlreadyRegistered:
         pass

0πŸ‘

from django.contrib import admin
from .models import Projects, ProjectsUsers, Comments, ProjectsDescription

Models = (Projects, ProjectsUsers, Comments, ProjectsDescription)

admin.site.register(Models)
πŸ‘€Igit Manukyan

0πŸ‘

From Django3.0,you can try add the following code in admin.py

from . import models


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

for m in [your_model_name]:
    mod = getattr(models, m)
    admin_class = type('AdminClass', (ListAdminMixin, admin.ModelAdmin), {})
    try:
        admin.site.register(mod, admin_class)
    except admin.sites.AlreadyRegistered:
        pass

πŸ‘€Jay Tsui

Leave a comment