如何在Django管理工具的其他部分移动模型? [英] How to move models in other section in django admin tool?

查看:52
本文介绍了如何在Django管理工具的其他部分移动模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将模型移至Django管理站点的另一部分?

How to move models to another section in the Django admin site?

在我的应用程序模块 models.py 中,具有在后端部分的管理工具中显示的模型。我希望它们显示在名称为请求的另一部分中。

In my application module models.py, I have models that are displaying in admin tool in section called "Backend". I want them to display in another section under the name "Requests".

我尝试了以下操作

class TransportationRequest(models.Model):
   ...
   class Meta:
      app_label = _('Requests')
      db_table = 'backend_transportationrequest'

这是可行的,但是现在我对South遇到了问题,因为它正在创建迁移以删除所有这些模型。

It is works, but now I have issues with South as it is creating migrations to delete all of these models.

推荐答案

您当前的问题是,您正在尝试更改 app_label db_table ,最终更改了模型数据在数据库中的位置。默认情况下,数据库表生成为 [app_label] _ [model_name] (在您的情况下为 backend_transportationrequest ),因此当您同时修改这两个参数时,South会检测到该模型已被删除并再次创建,即使实际上并非如此。

Your current issue is that you are trying to change the app_label and db_table, which ends up changing the location of the model data within the database. By default, the database table is generated as [app_label]_[model_name] (backend_transportationrequest in your case), so when you modify both of these, South detects that the model has been removed and created again, even if this isn't actually the case.

1.7版本中引入的Django迁移框架应该已修复,因此检测到模型已移动(而不是删除和创建)。您可能需要通过与南部相同的方式来伪造迁移,这可以通过修改其生成的两个偏移来实现而不是实际上删除并创建表,而是重命名它们。

The Django migrations framework introduced in 1.7 should have fixed this, so it detects that the model was moved (instead of deleted and created). You may need to fake a migration along the same lines as this with south, which can be done by modifying the two mgirations it generates to not actually delete and create the tables, but rename them.

Django当前不允许您轻松执行此操作,因为管理站点希望每个应用程序都可以已注册的具有唯一的 app_label 。您可能运气好您的AppConfig的 label 属性,但这是特别不推荐的,并且在历史上已知会引起严重的头痛。

Django does not currently allow you to easily do this, as the admin site expects that each application that is registered has a unique app_label. You may have luck playing with the label property of your AppConfig, but this is specifically not recommended and has been historically known to cause massive headaches.

一种可能是创建您先前模型的克隆,并仅使用它来向Django管理员注册应用。您需要使用以下方法创建代理模型自定义 app_label db_table 。如果这不起作用(尽管应该这样做),另一种选择是将模型克隆为使用 app_label db_table 的非托管模型

One possibility may be to create a clone of your previous model, and only use it to register the app with the Django admin. You would need to create a proxy model with the custom app_label and db_table. If this didn't work (though it should), the other option would be to clone the model as a unmanaged model using the app_label and db_table.

这篇关于如何在Django管理工具的其他部分移动模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆