Hi there!

I have the following model:

  class Visit(models.Model):
      user = models.ForeignKey(User)
      visitor = models.ForeignKey(User, related_name="visitor")
      last_visit = models.DateTimeField(default=timezone.now())
      count_visits = models.IntegerField(default=0)

I would like to make the following SQL query using the Django ORM:

  SELECT last_visit, SUM(count_visits) AS total_count_visits
  FROM core_visit
  WHERE user_id=42
  GROUP BY user_id
  ORDER BY last_visit DESC

So far, I haven't been able to do it. When I use values('user') and
order_by('-last_visit') the last_visit column is automatically added
to the GROUP BY clause. I've read in the Django doc that this is how
it's supposed to be but I can't seem to understand it. Why do I have
to group the results by whatever I choose as order? What am I missing?

Also, I know I can do a RawQuerySet but unfortunately, I would like to
be able to use a full QuerySet (this query goes into Tastypie and so
far, I haven't been successful in using it with anything but full
QuerySets, not even ValueQuerySets...)

All help and ideas would be greatly appreciated.

Thanks a lot in advanced,

-- 
J. Javier Maestro <jjmaes...@ieee.org>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to