Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group models from different app/object into one Admin block

Is it possible to group models from different apps into 1 admin block?

For example, my structure is

project/
  review/
    models.py -  class Review(models.Model):
  followers/
    models.py -  class Followers(models.Model):
    admin.py 

In followers/admin.py, I call

 admin.site.register(Followers)
 admin.site.register(Review)

This is to group them inside 1 admin block for administrators to find things easily.

I tried that, but Review model isn't showing up inside Followers admin block and I couldn't find documentation about this.

like image 500
DavidL Avatar asked May 12 '12 04:05

DavidL


People also ask

How do I import a model into admin py?

You can check the admin.py file in your project app directory. If it is not there, just create one. Edit admin.py and add below lines of code to register model for admin dashboard. Here, we are showing all the model fields in the admin site.

What is the purpose of the admin site in a Django project?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.

Is it possible to create a custom admin view without a model behind it?

An admin view without a model is basically just a normal Django view using an admin template with a mock ModelAdmin instance, which is basically what you're doing. I've done this myself to create some custom admin-themed pages.

What is Django administration?

django-admin is Django's command-line utility for administrative tasks. This document outlines all it can do. In addition, manage.py is automatically created in each Django project.


1 Answers

Django Admin groups Models to admin block by their apps which is defined by Model._meta.app_label. Thus registering Review in followers/admin.py still gets it to app review.

So make a proxy model of Review and put it in the 'review' app

class ProxyReview(Review):     class Meta:         proxy = True             # If you're define ProxyReview inside review/models.py,         #  its app_label is set to 'review' automatically.         # Or else comment out following line to specify it explicitly                        # app_label = 'review'          # set following lines to display ProxyReview as Review         # verbose_name = Review._meta.verbose_name         # verbose_name_plural = Review._meta.verbose_name_plural   # in admin.py admin.site.register(ProxyReview) 

Also, you could put Followers and Review to same app or set same app_label for them.

Customize admin view or use 3rd-part dashboard may achieve the goal also.

like image 67
okm Avatar answered Sep 24 '22 16:09

okm