With models defined like so:
class Athlete(models.Model):
name = models.CharField()
class Event(models.Model):
winner = models.ForeignKey(Athlete)
distance = models.FloatField()
type_choices = [('LJ', 'Long Jump'), ('HJ', 'High Jump')]
type = models.CharField(choices=type_choices)
I want to run a query picking out all the events an Athlete
has won, grouped by type. I'm currently doing it like so:
athlete = Athlete.objects.get(name='dave')
events_by_type = Events.objects.values('type').annotate(Count('winner')).filter(winner=athlete)
This gives me a dictionary of event types (short versions) and the number of times the athlete has been the winner. However that's all it gives me. If I then want to dig into one of these events to find the distance or even just the verbose type name, I can't.
Am I going about this in the right way? How can I get the events grouped, but also with access to all their fields as well?