Suppose that there is such a structure:
PARTICIPATION_STATUSES = ( (0, 'No, thanks'), (1, 'I may attend'), (2, 'I\'ll be there'), ) class Model1(models.Model): # ... class Model2(models.Model): status = models.PositiveIntegerField( _('participation status'), choices=PARTICIPATION_STATUSES) field = models.ForeignKey(Model1, related_name='model1_participation')
What I want to do is annotate each Model1 object with the number of Model2 objects where the status is equal to a certain value (the status number is a concrete example).
In my pseudo code, it would look like this:
queryset = Model1.objects.all() queryset.annotate(declined=Count('model1_participation__status=0')) queryset.annotate(not_sure=Count('model1_participation__status=1')) queryset.annotate(accepted=Count('model1_participation__status=2'))
But I cannot annotate the request this way, since Django does not allow status=<n> .
What is the right way to achieve what I want?
source share