On Fri, 22 May 2009 18:28:57 -0700 (PDT) Don Spaulding <donspauldin...@gmail.com> wrote:
> > > > On May 22, 8:16 pm, Don Spaulding <donspauldin...@gmail.com> wrote: > > On May 22, 1:13 pm, Laurent Meunier <laur...@deltalima.net> wrote: > > > > > > > > > Hi list! > > > > > I'm looking for a way to have the number of objects for each date > > > in the date_list object for the generic view > > > date_based.archive_index. > > > > > For example: > > > - 2009: 12 objects > > > - 2008: 10 objects > > > > > I saw that the generic view is using the method > > > mymodel.objects.dates(), maybe I can override it. But how? > > > > > For now, I'm stick with a static method in my model Class. This > > > method uses raw sql. > > > > > @staticmethod > > > def years(): > > > from django.db import connection > > > cursor = connection.cursor() > > > cursor.execute(" \ > > > SELECT p.published_date, COUNT(*) \ > > > FROM phog_photo p \ > > > WHERE p.is_published = 1 AND p.published_date <= '%s' > > > \ GROUP BY YEAR(published_date) \ > > > ORDER BY 1 DESC" % datetime.now().strftime("%Y-%m-%d > > > %H:%M:%S")) result_list = [] > > > for row in cursor.fetchall(): > > > y = (row[0], row[1]) > > > result_list.append(y) > > > return result_list > > > > > Is there a better and more elegant way to do this? I would like to > > > avoid raw sql as far as possible. > > > > > Thanks. > > > > > -- > > > Laurent Meunier <laur...@deltalima.net> > > > > I'm sure there's a better way, but are you looking for something > > like this? > > > > {% regroup latest by published_date as year_list %} > > > > {% for year_group in year_list %} > > Year: {{ year_group.grouper }} Num photos: {{ year_group.list| > > length }} > > {% endfor %} > > Doh! Just realized that'll get them grouped by the date, not the > date's year. An easy, though not necessarily elegant, fix for that > would be to group by a published_year property on your model, that > simply looked up self.published_date.year. Heading down this trail, > you come within throwing distance of SQL-looks-cleaner-ville ;-) Hi Don, Many thanks for your message. I've done some tests with the tag regroup as you suggested to me, and found three issues. First issue: The 'latest' object contains only objects for the current view (ie: not all objects if paginated). I have solved this issue by adding a new object to extra_context. Second issue: the variable year_group.grouper is not a datetime.date object. I can't do something like {{ year_group.grouper|date:"M, Y" }}. Third issue: if I group by month (or day) there are some cases where month are grouped over multiple years. For example: - first object: 2009/05/13 - second object: 2009/05/11 - third object: 2008/05/28 All three objects are grouped together. I would like to have two groups (one for 2009 and one for 2008). I solved the last two issues by adding three new methods in my model: def published_year(self): return date(self.published_date.year, 1, 1) def published_month(self): return date(self.published_date.year, self.published_date.month, 1) def published_day(self): return date(self.published_date.year, self.published_date.month, self.published_date.day) (the trick is to return a datetime.date object) Now I can do something like this in my template: {% regroup photos by published_month as grouped %} {% for p in grouped %} {{ p.grouper|date:"M, Y" }} ({{ p.list|length }}) {% endfor %} I have solved a template issue by modifying my model, not very elegant but I can live with it. However, I have dropped my RAW sql, so now I'm a happy django user :) -- Laurent Meunier <laur...@deltalima.net> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---