我有两个相同类(模型)类型的对象。我想检查 M2M 字段上的交叉点。有没有一种不使用原始 SQL 的有效方法来执行此操作?
基本上,这是我当前的解决方案。
genres_a = [g for g in profile_a.genres.all()] # Convert to List
genres_b = set([g for g in profile_b.genres.all()]) # Convert to Set
if genres_b.intersection(genres_a): # Look for Intersection (Not Lazy)
print True # Do something...
最佳答案
如果我正确理解交集:
profile_a.genres.all().filter(id__in=profile_b.genres.all())
关于python - 在 Django 中检查 M2M 交叉点的有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9807500/