python - django, how to Use Count in Q -
i have following models:
class group(models.model): group_name = models.charfield(max_length=16) class member(models.model): group = models.foreignkey('group') member_name = models.charfield(max_length=16) gender = models.booleanfield() #false "female", true "male"
how can find group
has no member
, or number of male member
s zero?
i tried following, didn't work:
group.objects.filter( q(member__isnull=true) | q(member__isnull=false) & q( count(member__gender=true)=0 ) )
solution,
group.objects.filter(q(member__isnull=true) | ~q(member__gender=true))
when you're trying fetch might return multiple results, should use filter , not get
if want count query set, can use,
group.objects.filter(q(member__gender=false )).count()
Comments
Post a Comment