如何在 Django 中正确分配 through 属性?

how to correctly assign the through attribute in django?

我只是在查看能够更改中间值的文档 table 但是当我实现它时,我遇到了麻烦:

https://docs.djangoproject.com/en/2.0/topics/db/models/#extra-fields-on-many-to-many-relationships

这样的问题是,虽然我可以迁移数据库和 运行 应用程序,但是当我进入管理员时,我没有通过 trough 属性(尤其是字段)正确地可视化模型的关系我的模型名为 Tested)。

为什么会发生这种情况,如何纠正?

这是设计使然。 Django 无法为使用 through table 的 ManyToMany 关系自动生成小部件,因为需要额外的数据(在您的情况下为 tested)。来自 Django docs:

When you specify an intermediary model using the through argument to a ManyToManyField, the admin will not display a widget by default. This is because each instance of that intermediary model requires more information than could be displayed in a single widget, and the layout required for multiple widgets will vary depending on the intermediate model.

However, we still want to be able to edit that information inline. Fortunately, this is easy to do with inline admin models.

最好的办法是按照文档中的说明创建一个内联管理模型。