gpt4 book ai didi

python - 如何计算 Django 多对多关系中包含特定值的项目

转载 作者:行者123 更新时间:2023-12-05 04:41:27 32 4
gpt4 key购买 nike

我有一个用于注释的 Django 应用程序,我通过多对多关系将有关图像注释的信息保存到模型 BiomarkerAnnotations 中。我跟踪哪些用户已使用 annotated 字段完成图像注释。

from django.contrib.auth.models import User

class Biomarker(models.Model):
name = models.CharField(max_length=256)
description = models.CharField(max_length=1024)

class Image(models.Model):
number = models.IntegerField(default=0)
absolute_path = models.CharField(max_length=2560)
biomarkers = models.ManyToManyField(Biomarker, through='BiomarkerAnnotations', related_name="biomarker_annotations")
annotated = models.ManyToManyField(User, related_name="annotated_by")

class BiomarkerAnnotations(models.Model):
_image = models.ForeignKey(Image, on_delete=models.CASCADE)
biomarker = models.ForeignKey(Biomarker, on_delete=models.CASCADE)
user = models.ForeignKey(User, null=True,on_delete=models.SET_DEFAULT, default=1)

我想创建一个 View ,为特定用户(发送请求的用户)返回他已经注释了多少图像以及还有多少图像需要注释。到目前为止,我已经达到了这一点,但它似乎不起作用:查询返回的总计数大于图像计数。

class AnnotStatsSerializer(serializers.ModelSerializer):
annotations = serializers.IntegerField()
count = serializers.IntegerField()

class Meta:
model = Image
fields = ("count", "annotations")

class AnnotatedStatsViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Image.objects.all()
serializer_class = AnnotStatsSerializer


def get_queryset(self):
queryset = self.queryset
user_object = self.request.user

project_id = self.request.query_params.get('project', None)
if project_id is None: raise Http404

queryset = queryset.annotate(annotations=Case(When(annotated__id__exact=user_object.id, then=Value(1)), default=Value(0), output_field=IntegerField())) \
.values('annotations').annotate(count=Count('annotations')).order_by('annotations') \
.values('count', 'annotations')

return queryset

感谢任何帮助。

最佳答案

您可以使用:

annotated_images = Image.objects.filter(
<strong>biomarkerannotations__user=user_object</strong>
)<strong>.count()</strong>
images_todo = Image.objects.exclude(
<strong>biomarkerannotations__user=user_object</strong>
)<strong>.count()</strong>

获取annotated_imagesimages_todo的个数。

或者如果您正在使用带注释的多对多关系:

annotated_images = Image.objects.filter(
<strong>annotated=user_object</strong>
)<strong>.count()</strong>
images_todo = Image.objects.exclude(
<strong>annotated=user_object</strong>
)<strong>.count()</strong>

我们可以让它与一组用户一起工作:

from django.db.models import Count, OuterRef, Subquery, Value

User.objects.annotate(
tagged=Count('annotated_by'),
not_tagged=Subquery(
Image.objects.exclude(
annotated=OuterRef('pk'),
).values(foo=Value(None)).values(
total=Count('pk')
).order_by(Value(None))
)
)

这会生成如下所示的查询:

SELECT auth_user.*,
COUNT(app_name_image_annotated.image_id) AS tagged
(
SELECT COUNT(V0.id) AS total
FROM app_name_image V0
WHERE NOT EXISTS
(
SELECT (1) AS a
FROM app_name_image_annotated U1
WHERE U1.user_id = auth_user.id AND U1.image_id = V0.id
LIMIT 1
)
) AS not_tagged FROM auth_user
LEFT OUTER JOIN app_name_image_annotated ON (auth_user.id = app_name_image_annotated.user_id)
GROUP BY auth_user.*

关于python - 如何计算 Django 多对多关系中包含特定值的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70086869/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com