Hi there! Thanks a lot for the reply. However, I don't think your solution would work in my case, nor it answers the question of whether it can be done or why the order_by() adds stuff to the GROUP BY clause when used in conjunction with values().
Let's see: On Tue, Jul 3, 2012 at 11:08 PM, Bill Freeman <ke1g...@gmail.com> wrote: > First, I think that you're going about last_visit in the wrong way. > timezone.now() is evaluated once, when the module is imported, > rather than when the user visits. DateTimeField's auto_now=True > option is tailor made for this kind of situation. I know about auto_now and, apart from the discussions that can be found all over the Internet, is a nice feature... unless you update records (like I do). Then, auto_now does nothing since it only affects save(). In any case, the default value is actually a leftover from the initial implementation, right now is not even needed. > Next, since you have the user id, I'm going to assume that you have > an actual user object. In the code below I'll presume that it's > request.user . > > visits_qs = Visit.objects.filter(user=request.user) > total_visits_count = visits_qs.count() > latest_visit = visits_qs.order_by('-last_visit')[0] > > Yes, this is two database queries, but unless you are seeing a performance > issue that you've traced to the fact that this is more than one query, this > code is a heck of a lot more maintainable than trying to make it happen > in one. First, this won't work in my implementation. My model stores the actual count of a given visitor in the count_visits field and the field is updated every time the visitor visits a user profile (via a middleware and an F() expression) while your solution stores a full record every time you want to log a visit to a user profile. Logging visits your way will eat quite a bit of space in the database. Also, it's not only about saving space and it's not only about two database queries. In your implementation, the count() will scan the whole set of records for the given user. This scan will grow with the number of records and in this case, taking into account that the whole purpose is to show a counter for visits to a given user profile... I think that your proposed workaround does not really fit. So, I ask again: Why the order_by('colum') when used in conjunction with values(), adds the column to the GROUP BY? Thanks, -- 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.