Django - Annotate with count across ManytoMany relationships

白昼怎懂夜的黑 提交于 2020-01-25 09:31:08

问题


I am not able to find the way to annotate a queryset with a count of how many times an element is used in a many-to-many relationship.

class Profile(models.Model):
    [...]
    # Profile can have multiple roles
    roles = models.ManyToManyField('Role', blank=True)
    [...]

class Role(models.Model):
    company = models.ForeignKey(Company, on_delete=models.CASCADE)
    name = models.CharField(blank=True, max_length=30)
    description = models.CharField(blank=True, max_length=300)
    [...]   

For example I would have 5 roles:

  • Role1
  • Role2
  • Role3
  • Role4
  • Role5

And 2 profiles with following roles assigned:

  • Profile1
    • Role 1
    • Role 2
  • Profile2
    • Role 1
    • Role 3
    • Role 4

I want to query the Role model and annotate with the number of profile that have that role.

So return a queryset like

Role1: company, name, description, profile_count=2
Role2: company, name, description, profile_count=1

etc...

I have tried that but it does not work:

Role.objects.annotate(profile_count=Count('profile__roles'))

It seems to return an overall count and not a count per role.

Any idea if that can be done natively in Django or if a raw SQL request is necessary?

Thanks!


回答1:


If i understand you correctly you just want this:

Role.objects.annotate(profile_count=Count('profile'))

Which is almost what you wrote.



来源:https://stackoverflow.com/questions/52733085/django-annotate-with-count-across-manytomany-relationships

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!