Django:多对多,通过vs 2x一对多

时间:2022-06-03 09:58:17

Reading, the Django docs on many-to-many relationships when you need an extra field on the intermediary table, it's not clear to me what you gain by defining the relationship as many-to-many vs just having a foreign key to the intermediary model in each of your models.

阅读,当你需要在中间表上增加一个字段时,Django会记录多对多关系,我不清楚你通过将关系定义为多对多而只是向中介使用外键来获得什么每个模型中的模型。

For instance, in the example here: https://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany

例如,在这里的示例中:https://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany

Why bother with the ManyToManyField line? Does it allow you to refer to the relationship any differently? Does it change something in the admin?

为什么要烦扰ManyToManyField线?它是否允许您以不同的方式引用关系?它会改变管理员的内容吗?

1 个解决方案

#1


1  

Why bother with the ManyToManyField line: M2M fields are django fields which are useful for many model field interfaces like ModelAdmin or ModelForm (declaring fields, excluding, default widgets). It also generate helper methods like easy to access m2m managers and auto m2m saving in the admin.

为什么要使用ManyToManyField行:M2M字段是django字段,对于许多模型字段接口(如ModelAdmin或ModelForm)(声明字段,不包括默认窗口小部件)非常有用。它还生成辅助方法,如易于访问m2m管理器和管理员中的自动m2m保存。

Here's a quick example for what the field does automatically for the admin panel: you can't save a through model without the main object saved first. The admin handles this behavior with a save_m2m after the main object is saved automatically. If you did not use an m2m field, you'd have to code this kind of logic yourself.

以下是该字段为管理面板自动执行的操作的快速示例:如果没有首先保存主对象,则无法保存直通模型。在自动保存主对象后,管理员使用save_m2m处理此行为。如果你没有使用m2m字段,你必须自己编写这种逻辑。

I think you'd use this field when your project works with m2m fields but you just need a small amount of additional information. That way your code is virtually the same as a typical m2m relationship but when you need it, you can query the extra info.

我认为当你的项目使用m2m字段时你会使用这个字段,但你只需要少量的附加信息。这样你的代码几乎与典型的m2m关系相同,但是当你需要它时,你可以查询额外的信息。

Otherwise, adding a mere "date_added" field would require writing admin widgets, save_model hooks, ModelForm representation, etc.

否则,添加仅仅“date_added”字段将需要编写管理窗口小部件,save_model挂钩,ModelForm表示等。

You're right though that removing the m2m line with a through model won't make any database changes and you could ultimately replace the m2mfield functionality with the default ForeignKey reverse managers without much harm.

你是对的,虽然用直通模型删除m2m行不会进行任何数据库更改,你最终可以用默认的ForeignKey反向管理器替换m2mfield功能而不会造成太大的伤害。

model.m2m_through_set.all() # this would work
model.m2m_through_set.create(target=target_instance)

#1


1  

Why bother with the ManyToManyField line: M2M fields are django fields which are useful for many model field interfaces like ModelAdmin or ModelForm (declaring fields, excluding, default widgets). It also generate helper methods like easy to access m2m managers and auto m2m saving in the admin.

为什么要使用ManyToManyField行:M2M字段是django字段,对于许多模型字段接口(如ModelAdmin或ModelForm)(声明字段,不包括默认窗口小部件)非常有用。它还生成辅助方法,如易于访问m2m管理器和管理员中的自动m2m保存。

Here's a quick example for what the field does automatically for the admin panel: you can't save a through model without the main object saved first. The admin handles this behavior with a save_m2m after the main object is saved automatically. If you did not use an m2m field, you'd have to code this kind of logic yourself.

以下是该字段为管理面板自动执行的操作的快速示例:如果没有首先保存主对象,则无法保存直通模型。在自动保存主对象后,管理员使用save_m2m处理此行为。如果你没有使用m2m字段,你必须自己编写这种逻辑。

I think you'd use this field when your project works with m2m fields but you just need a small amount of additional information. That way your code is virtually the same as a typical m2m relationship but when you need it, you can query the extra info.

我认为当你的项目使用m2m字段时你会使用这个字段,但你只需要少量的附加信息。这样你的代码几乎与典型的m2m关系相同,但是当你需要它时,你可以查询额外的信息。

Otherwise, adding a mere "date_added" field would require writing admin widgets, save_model hooks, ModelForm representation, etc.

否则,添加仅仅“date_added”字段将需要编写管理窗口小部件,save_model挂钩,ModelForm表示等。

You're right though that removing the m2m line with a through model won't make any database changes and you could ultimately replace the m2mfield functionality with the default ForeignKey reverse managers without much harm.

你是对的,虽然用直通模型删除m2m行不会进行任何数据库更改,你最终可以用默认的ForeignKey反向管理器替换m2mfield功能而不会造成太大的伤害。

model.m2m_through_set.all() # this would work
model.m2m_through_set.create(target=target_instance)