Group models from different app/object into one Ad

2020-06-07 04:35发布

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.

1条回答
在下西门庆
2楼-- · 2020-06-07 05:23

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.

查看更多
登录 后发表回答