用manytomy字段的聚合状态注释模型



我有一个流,它的权限=ManyToManyField(权限(权限有一个状态字段。如果所有流的权限状态都为True,则用allowed=True注释流,否则用allowe=False 注释流

from django.db import models
class Permission(models.Model):
name = models.CharField(max_length=10),
state = models.BooleanField()
class Stream(models.Model):
permissions = models.ManyToManyField(Permission)
p1 = Permission.objects.create(state=True)
p2 = Permission.objects.create(state=False)
s = Stream.objects.create(name='s')
s.permissions.add([p1, p2])
s1 = Stream.objects.create(name='s1')
s1.permissions.add(p1)
s2 = Stream.objects.create(name='s2')
s2.permissions.add(p2)
# How can I do something like this?
annotated_streams = Stream.objects.annotate(allowed={... all([p.state for p in permissions]) ...})
[(s.name, s.allowed) for s in annotated_streams]
[('s', False), ('s1', True), ('s2', False)]

如果我正确理解你的问题,这应该是

from django.db.models import Count, Case, When, BooleanField, Q
annotated_streams = Stream.objects.alias(
fcount = Count('permissions', filter=(Q(permissions__state=False)),
).annotate(
allowed = Case(
When(Q(fcount__gt=0), then=False)
default = True, output_field=BooleanField()
)
)

相关内容

  • 没有找到相关文章

最新更新