Django annotate count with a distinct field

前端 未结 4 358
面向向阳花
面向向阳花 2020-11-29 05:49

I have two models defined loosely like this:

class InformationUnit(models.Model):
    username = models.CharField(max_length=255)
    project = models.Foreig         


        
4条回答
  •  天涯浪人
    2020-11-29 06:27

    If you just want to count the distinct values, you can use the distinct() and count() functions:

    count = Project.objects.values('informationunit__username').distinct().count()
    

提交回复
热议问题