How can I annotate a Django queryset with a count in a way that's conditional to some property of each item in the queryset? -
sorry title, think pseudo-code clarify question:
class image(model): user = foreignkey(user) class group(model): members = foreignkey(user) images = foreignkey(image) autosubmission = booleanfield()
as can see, have group
, consisting of members
, images
.
if group autosubmission
members don't submit images manually it, images belonging member automatically show in group.
if group not autosubmission
members need submit images manually, , end in images
foreignkey
.
now, want have queryset that's annotated number of images the group.
group.objects.all().annotate(num_images = count('images'))
obviously, code above works groups not autosubmission
, i.e. images in images
relation.
so how can annotate queryset works both groups autosubmission
, groups not?
thanks!
Comments
Post a Comment