I have these two Django models (simplified):
class Movie(models.Model):
title = models.CharField(max_length=255)
year = models.IntegerField(max_length=4)
rating = models.DecimalField(max_digits=2, decimal_places=1, default=0)
class Request(models.Model):
movie = models.ForeignKey(Movie)
user = models.ForeignKey(User)
language = models.CharField(max_length=7, choices=settings.APP_LANGUAGES, db_index=True)
notes = models.CharField(max_length=255, blank=True, null=True)
added = models.DateTimeField(default=datetime.now)
I want to get all Requests with a count by language for each movie. I actually come up with a solution with an ORM query, which gives me the results I am looking for:
reqs = Request.objects.values('movie', 'langauge').annotate(Count('language'))
However, I will need an access to other Movie attributes while I will be rendering the results in the template. Obviously, the above query returns ValueQuerySet like:
[{'movie': 460, 'language__count': 1, 'language': u'es-mx'}, {'movie': 458, 'language__count': 2, 'language': u'cs'}, {'movie': 459, 'language__count': 1, 'language': u'el'}]
Thus something like reqs[0].movie.rating won't work. So, how should I access other Movie attributes?
Thank you.
reqs = Request.objects.annotate(Count('language')).select_related('movie')?