Django Admin:在多对多关系中将添加到* other *模型

时间:2022-10-04 14:10:43

I have a many-to-many relationship in my application between Tools and Tasks. Right now the many-to-many relationship is defined on the Task model, such that in the Task edit page of the admin I have a <select multiple> control that allows you to pick 0 or more Tools to associate with the Task.

我的应用程序在工具和任务之间有多对多的关系。现在,在任务模型上定义了多对多关系,这样在管理员的任务编辑页面中我有一个

I know that Django does not allow you to define the relationship on both models, but is it still possible to tell the admin to include a <select multiple> on the Tool edit page as well? I would like to be able to edit the relationship from either side of it.

我知道Django不允许你在两个模型上定义关系,但是仍然可以告诉管理员在工具编辑页面上包含

3 个解决方案

#1


1  

Something like this should work (given m2m is specified in Tool):

这样的东西应该工作(给定m2m在工具中指定):

# 1st app
from secondapp.models import Tool

class Task(models.Model):
    tools = models.ManyToManyField(Tool, through=Tool.tasks.through)

# 2nd app
class Tool(models.Model):
    tasks = models.ManyToManyField("firstapp.Task")

#2


0  

You could try creating a dummy model between Tools and Tasks that acts as an intermediary, something Django offers normally for use in storing extra information about a particular relationship. You would use the through= keyword when creating this ManyToMany relationship to make use of that.

您可以尝试在作为中介的工具和任务之间创建一个虚拟模型,这是Django通常提供的用于存储特定关系的额外信息的东西。在创建此ManyToMany关系时,您将使用through =关键字来使用它。

You can find details on that here.

你可以在这里找到相关细节。

I haven't actually taken a look at it on the admin page, but I would imagine once you register the model on the admin page, because that model has a ForeignKey relationship to each of Tools and Tasks, you should be able to edit the relationship on both sides from the dummy model.

我实际上没有在管理页面上看过它,但我想在管理页面上注册模型之后,因为该模型与每个工具和任务都有一个ForeignKey关系,你应该能够编辑虚拟模型两侧的关系。

I wouldn't necessarily recommend this, though, unless it's super essential that you be able to access the relationship on both sides.

但是,我不一定会推荐这个,除非你能够访问双方的关系是非常必要的。

#3


0  

I would not use through or complicate your model just for the sake of getting the admin to look fine. You may alternatively try creating an admin inline. With inlines, you can add subsections to any model's screen in the admin application. There are some good examples in the docs.

我不会仅仅为了让管理员看起来很好而使用或复杂化你的模型。您也可以尝试创建内联管理员。使用内联,您可以在管理应用程序中的任何模型屏幕上添加子部分。文档中有一些很好的例子。

#1


1  

Something like this should work (given m2m is specified in Tool):

这样的东西应该工作(给定m2m在工具中指定):

# 1st app
from secondapp.models import Tool

class Task(models.Model):
    tools = models.ManyToManyField(Tool, through=Tool.tasks.through)

# 2nd app
class Tool(models.Model):
    tasks = models.ManyToManyField("firstapp.Task")

#2


0  

You could try creating a dummy model between Tools and Tasks that acts as an intermediary, something Django offers normally for use in storing extra information about a particular relationship. You would use the through= keyword when creating this ManyToMany relationship to make use of that.

您可以尝试在作为中介的工具和任务之间创建一个虚拟模型,这是Django通常提供的用于存储特定关系的额外信息的东西。在创建此ManyToMany关系时,您将使用through =关键字来使用它。

You can find details on that here.

你可以在这里找到相关细节。

I haven't actually taken a look at it on the admin page, but I would imagine once you register the model on the admin page, because that model has a ForeignKey relationship to each of Tools and Tasks, you should be able to edit the relationship on both sides from the dummy model.

我实际上没有在管理页面上看过它,但我想在管理页面上注册模型之后,因为该模型与每个工具和任务都有一个ForeignKey关系,你应该能够编辑虚拟模型两侧的关系。

I wouldn't necessarily recommend this, though, unless it's super essential that you be able to access the relationship on both sides.

但是,我不一定会推荐这个,除非你能够访问双方的关系是非常必要的。

#3


0  

I would not use through or complicate your model just for the sake of getting the admin to look fine. You may alternatively try creating an admin inline. With inlines, you can add subsections to any model's screen in the admin application. There are some good examples in the docs.

我不会仅仅为了让管理员看起来很好而使用或复杂化你的模型。您也可以尝试创建内联管理员。使用内联,您可以在管理应用程序中的任何模型屏幕上添加子部分。文档中有一些很好的例子。