Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an app named doors and my models.py for the app has 10 tables/class. Under my admin.py, how do I register every model in the file models.py?

For example, currently I have to hardcode it:

from django.contrib import admin
from doors.models import *

admin.site.register(Group)
admin.site.register(Item)
admin.site.register(ItemType)
admin.site.register(Location)
admin.site.register(Log)
admin.site.register(Order)
admin.site.register(Property)
admin.site.register(User)
admin.site.register(Vendor)

Is there a way I perhaps find every class in models.py and loop through and register each class? Or is there some kind of wildcard I can use with Django?

like image 650
hobbes3 Avatar asked Feb 25 '12 12:02

hobbes3


People also ask

How do I automatically register all models in Django admin?

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. This will fetch all the models in all apps and registers them with the admin interface.

What is Admin Modeladmin in Django?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.


2 Answers

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.

like image 164
mehmet Avatar answered Oct 25 '22 09:10

mehmet


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?

like image 35
hobbes3 Avatar answered Oct 25 '22 10:10

hobbes3