Friday, April 22, 2011

ORM help

Hello guys,

I have a following models:

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, 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 count per each pair of Movie and language.

So far I got this:
Request.objects.values('movie', 'language').annotate(Count('language'))

That seems to give me almost what I want, but that way I get a
ValueQuerySet. Is there a way to get or convert this to a QuerySet? I'd
like to get an easy way to access Movie attributes from the above query
as I will need to display movie title and rating in the template for
each result from the above query.

So, is there some better way of what am I trying to accomplish? If not,
how should I proceed in retrieving the Movie attributes? Will I need to
fetch those with something like Movie.objects.filter(pk__in=[list of
ids]) and then manually map those?

Thanks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate