我有以下型号。
class Document(models.Model):
allowed_groups = models.ManyToManyField(Group, related_name='allowed_documents')
class Person(models.Model):
permission_groups = models.ManyToManyField(Group, related_name='people')
class Group(models.Model):
id = models.BigIntegerField()
我想找到人员可以访问的所有文档,条件是他们必须是所有允许组的成员。
我想要这个: 案例
如果我这样做:
person = Person.objects.get(pk=1)
Document.objects.filter(allowed_groups__in=person.permission_groups.all())
我会匹配上述所有情况,除了 8 个(不是我想要的)
有很多关于堆栈溢出的问题询问精确匹配,即仅匹配情况 6 而不是情况 1。(也不是我想要的)
所以我的问题是如何使用 django 来做到这一点?我考虑过使用 SQL,但肯定有一种方法可以使用 Django ORM。这似乎并不是一个疯狂的要求。
注意:我还有一些其他条件(其他类型的组和文档访问级别),我已将其转换为带有链式过滤器/Q 对象的复杂表达式,但除了这一点之外,我已经解决了所有问题。
另外:我在表述我的问题标题时遇到了一些麻烦,这可能就是我找不到答案的原因。它不需要查询集,它可能只是 pks 列表或其他方法。
基于这个答案
解决方案:
from django.db.models import Count, Q
person = Person.objects.get(pk=1)
permission_groups = set(person.permission_groups.all())
Document.objects.annotate(
allowed_groups_count=Count('allowed_groups', filter=Q(allowed_groups__in=permission_groups))
).filter(
allowed_groups_count__gt=0
)
然后它的查询将是这样的:
SELECT
document.id,
COUNT(document_allowed_groups.group_id) FILTER (
WHERE
document_allowed_groups.group_id IN (1, 2, 6, 7, 11, 15)
) AS allowed_groups_count
FROM
document
LEFT OUTER JOIN document_allowed_groups ON (
document.id = document_allowed_groups.document_id
)
GROUP BY
document.id
HAVING
COUNT(document_allowed_groups.group_id) FILTER (
WHERE
(
document_allowed_groups.group_id IN (1, 2, 6, 7, 11, 15)
)
) > 0
使用 numpy 库中的 np.isin 函数。比较两个数组时,它返回布尔数组。描述在这里。
我将values_list和flat=True应用于对象,将它们提取到列表中以便在numpy中进行比较。我做了一个列表生成器aaa(列表生成器比循环快很多倍),其中将文档与所选人员进行比较,如果所有文档值匹配,则all()返回True,然后将i.pk写入aaa列表。接下来,文档数据将通过此列表进行过滤并传递到字典以在模板中显示。
将 bboard 替换为放置模板的文件夹的名称。 我有这个:templates/bboard,位于应用程序文件夹中。
views.py
import numpy as np
def info(request):
person = Person.objects.get(pk=1).permission_groups.all().values_list('id', flat=True)
print(person)
def my_func(x):
document = Document.objects.get(pk=x).allowed_groups.all().values_list('id', flat=True)
return document
aaa = [i.pk for i in Document.objects.all() if np.isin(my_func(i.pk), person).all()]
document = Document.objects.filter(pk__in=aaa)
return render(request, 'bboard/templ.html', {'context': document})
模板
{% for a in context %}
<p>{{ a.id }}</p>
{% endfor %}
更新2022年10月26日
import numpy as np
def info(request):
person = Person.objects.get(pk=1).permission_groups.all().values_list('id', flat=True)
def my_func(i):
document = i.allowed_groups.all().values_list('id', flat=True)
return document
aaa = [i.pk for i in Document.objects.prefetch_related('allowed_groups').all() if np.isin(my_func(i), person).all()]
document = Document.objects.filter(pk__in=aaa)
return render(request, 'bboard/templ.html', {'context': document})