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

问题描述

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中查找不同元素数的正确查询是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)