Django m2m查询,与模型的m2m关系的不同用户

时间:2022-04-13 08:03:36

I have a model Model with a m2m field :

我有一个m2m字段的模型模型:

user = .. fk user
...
watchers = models.ManyToManyField(User, related_name="boardShot_watchers",  null=True)

How do i select all distinct Users involved in this watchers relationship for all my entries of type Model ?

如何为模型类型的所有条目选择此观察者关系中涉及的所有不同用户?

I dont think there is an ORM way to access to intermediary M2M table.

我不认为有一种ORM方式可以访问中间M2M表。

Greg

1 个解决方案

#1


Not in your current model. If you want to have explicit access to the joining table, you need to make it part of the Django object model. The docs explain how to do this:

不在您当前的型号中。如果要显式访问连接表,则需要将其作为Django对象模型的一部分。文档解释了如何执行此操作:

http://www.djangoproject.com/documentation/models/m2m_intermediary/

The admin and other django.contrib* components can be configured to treat most fields the same as if they were just model.ManyToMany's. But it will take a little config.

admin和其他django.contrib *组件可以配置为处理大多数字段,就像它们只是model.ManyToMany一样。但它需要一点配置。

#1


Not in your current model. If you want to have explicit access to the joining table, you need to make it part of the Django object model. The docs explain how to do this:

不在您当前的型号中。如果要显式访问连接表,则需要将其作为Django对象模型的一部分。文档解释了如何执行此操作:

http://www.djangoproject.com/documentation/models/m2m_intermediary/

The admin and other django.contrib* components can be configured to treat most fields the same as if they were just model.ManyToMany's. But it will take a little config.

admin和其他django.contrib *组件可以配置为处理大多数字段,就像它们只是model.ManyToMany一样。但它需要一点配置。