In my Tastypie resource, I'm annotating my queryset, and yet I don't see that annotation flow through to the JSON Tastypie generates and passes back. The code is straightforward:
class CompetitionResource(ModelResource):
total_tickets = fields.IntegerField(readonly=True)
class Meta:
queryset = Competition.objects.all().annotate(total_tickets=Count('ticket__ticketownership__user__id', distinct=True))
That Count I'm generating and annotating there in my queryset simply does not show up in the final JSON. The final JSON has a total_users field (because I declared one in my ModelResource), but it's null. Am I missing anything obvious to make sure annotations like this get passed through? If not, what would be a way to solve this?
One way to do it is to create an attribute in my Model and then tie the total_users field in my ModelResource to that attribute. But that would presumably result in a Count query for each individual Competition I pull from the database, and that is not good. I want to do it in one annotation-type query.
annotate()
used withall()
before. What happens if you just useCompetition.objects.annotate(total_tickets=Count('ticket__ticketownership__user__id', distinct=True))
// edit: I just looked at the Django code, and these calls should do the same thing...sorry. – Lacerated