如何使用Django的ORM过滤包含多对多关系中多个元素的所有对象?

时间:2018-03-29 23:54:39

标签: sql django orm django-orm

我在Django中有两个通过ManyToManyField链接的类(User类是内置的User模型):

from django.contrib.auth.models import User

class Activity():
    participants = models.ManyToManyField(User, related_name='activity_participants')

我想找到两个用户同时参与的所有活动。

我设法使用原始查询解决了我的问题(我的应用名称是"核心"因此"核心"表名中的前缀):

SELECT ca.id FROM core_activity_participants AS cap, core_activity AS ca
INNER JOIN core_activity_participants AS cap2 ON cap.activity_id
WHERE cap.user_id == 1 AND cap2.user_id == 2
    AND cap.activity_id == cap2.activity_id
    AND ca.id == cap.activity_id

但是,如果可能的话,我希望避免使用原始查询,因为它会破坏我应用程序的其余部分的一致性。如何使用Django的ORM进行此查询或等效查询?

1 个答案:

答案 0 :(得分:1)

如果您使用的是Django 1.11或更高版本,intersection queryset方法将为您提供所需的记录。

# u1 and u2 are User instances
u1_activities = Activity.objects.filter(participants=u1)
u2_activities = Activity.objects.filter(participants=u2)
common_activities = u1_activities.intersection(u2_activities)

会生成如下所示的查询:

SELECT "core_activity"."id"
FROM "core_activity"
INNER JOIN "core_activity_participants"
ON ("core_activity"."id" = "core_activity_participants"."activity_id")
WHERE "core_activity_participants"."user_id" = 1
INTERSECT
SELECT "core_activity"."id"
FROM "core_activity"
INNER JOIN "core_activity_participants"
ON ("core_activity"."id" = "core_activity_participants"."activity_id")
WHERE "core_activity_participants"."user_id" = 2

如果要检查两个以上用户之间的活动重叠,您还可以向交叉点添加额外的查询集。

更新

另一种适用于较旧的Django版本的方法是

u1_activities = u1.activity_participants.values_list('pk', flat=True)
common_activities = u2.activity_participants.filter(pk__in=u1_activities)

产生类似

的查询
SELECT "core_activity"."id"
FROM "core_activity"
INNER JOIN "core_activity_participants"
ON ("core_activity"."id" = "core_activity_participants"."activity_id")
WHERE (
    "core_activity_participants"."user_id" = 2
    AND "core_activity"."id" IN (
        SELECT U0."id"
        FROM "core_activity" U0
        INNER JOIN "core_activity_participants" U1
        ON (U0."id" = U1."activity_id")
        WHERE U1."user_id" = 1
    )
)