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 %}
--~--~---------~--~----~------------~-------~--~----~
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