如何在Django中使用注释功能?

时间:2020-08-11 08:18:03

标签: python-3.x django-models django-orm django-3.0

class Certificate(models.Model):
    id = models.AutoField(primary_key=True)
    creator = models.ForeignKey(Users_custom, null=True, on_delete=models.SET_NULL)
    issue_id =models.CharField()


class Users_custom(models.Model):
    id = models.AutoField(primary_key=True)
    organization_id =models.charField()
    first_name = models.CharField()

我要在下面执行此查询:

SELECT count(distinct(cer.issue_id)) FROM certificate as cer, users_custom as usr where usr.organization_id =9 and cer.creator_id = usr.id;

我使用注释功能尝试了此操作,但没有给我正确的计数:

Certificate.objects.filter(creator__organization=9).annotate(count =Count("issue_id",distinct =True)).count()

在Django中查找不同元素数的正确查询是什么?

0 个答案:

没有答案