Use开发者_开发问答r
id
name
Device
id
name
user-id
is_enabled
I want to select all Users who has
- At least one device enabled.
- Exactly 2 device enabled
- 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)
精彩评论