开发者

Django aggregate function problem (Count = -number-)

Use开发者_开发问答r
   id
   name
Device
   id
   name
   user-id
   is_enabled 

I want to select all Users who has

  1. At least one device enabled.

  2. Exactly 2 device enabled

  3. All devices enabled

    Note: enabled means (Device.is_enabled = 1)

I know I can use annotate to get number of devices. But how to use annonate to get number of users who has exactly 2 enabled devices,

I am currently messing around with

User.objects.filter('device_set__is_enabled'=1)

Its giving List of Users who have at least one enabled device. Dont know what to do next.


from django.db.models import Count
User.objects.filter(device__is_enabled=1).annotate(
    device_count=Count('device')).filter(device_count=2)

User.objects.exclude(device__is_enabled=0)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜