简体   繁体   English

如何在 django admin 中对模型进行分组?

[英]How to group models in django admin?

Lets say I have a group of models that must be kept separate (visually) in the admin from another group.假设我有一组模型必须在管理员中与另一个组分开(视觉上)。

Right now they are alphabetic, which jumbles them.现在它们是按字母顺序排列的,这使它们变得混乱。

I'd like to organize them this way:我想以这种方式组织它们:


Group 1: (custom named)第 1 组:(自定义命名)

  • Model 1模型 1
  • Model 4模型 4

Group 2 (custom named)第 2 组(自定义命名)

  • Model 2模型 2
  • Model 3模型 3

I cannot seem to find the documentation on how to do this.我似乎找不到有关如何执行此操作的文档。 Is this even possible?这甚至可能吗?

I do not think that splitting up your business logic, ie your app, when all you want to achieve is some kind of markup is the right way. 我不认为拆分你的业务逻辑,即你的应用程序,当你想要实现的是某种标记时,这是正确的方法。 Instead I found the Python package django-modeladmin-reorder that lets you achieve this easily. 相反,我找到了Python包django-modeladmin-reorder ,可以让你轻松实现这一目标。 You can combine its feature of labelling apps and reordering models to group models of one app in the admin. 您可以将其标记应用程序和重新排序模型的功能组合到管理员中的一个应用程序的组模型中。 After following the installation instructions, add something like this to your settings.py 按照安装说明操作后,在settings.py添加如下内容

ADMIN_REORDER = (
    # First group
    {'app': 'myapp', 'label': 'Group1',
     'models': ('myapp.Model_1',
                'myapp.Model_4',)
    },
    # Second group: same app, but different label
    {'app': 'myapp', 'label': 'Group2',
     'models': ('myapp.Model_2',
     'myapp.Model_3',)
    },)

Please make sure that your model names in ADMIN_REORDER have to be exact as written in your models.py file.请确保您在ADMIN_REORDER中的模型名称必须与您的 models.py 文件中所写的完全相同

For me, it worked with Django==3.1.5 without any issue.对我来说,它可以与 Django==3.1.5 一起使用,没有任何问题。 Here is my sample code in the settings.py file这是我在 settings.py 文件中的示例代码

ADMIN_REORDER = (
# 'webapp',

#### First group
{
    'app': 'webapp', 
    'label': 'group1',
    'models': (
        'webapp.ProductModelName_1', 
        'webapp.ProductModelName_2', 
        'webapp.ProductModelName_3',

    )
},
# Second group: same app, but different label
{
    'app': 'webapp', 
    'label': 'group2',
    'models': (
        'webapp.Model_X', 
        'webapp.Model_Y',
    )
},
)

You need to create two apps. 您需要创建两个应用程序。 first app == Group 1. second app == Group 2. 第一个app == Group 1.第二个app == Group 2。

Then, you need to create proxy model in your new app. 然后,您需要在新应用中创建代理模型 Something like this. 像这样的东西。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM