i guess i can use this:

def field_sql(table, field) : return '%s.%s' %
(connection.ops.quote_name(table), connection.ops.quote_name(field))

select_sql = connection.ops.date_trunc_sql('year', field_sql
(MyModel._meta.db_table, 'date'))
MyModel.objects.extra(select = {'year' : select_sql}).values
('year').order_by().annotate(total = Count('pk'))

the only problem it does not worj at least with sqlite because it
produces this query:

[{'time': '0.007', 'sql': u'SELECT (django_date_trunc("year",
"mymodel"."date")
) AS "year", COUNT("mymodel"."id") AS "total" FROM "mymodel" GROUP BY
django_d
ate_trunc("year", "mymodel"."date")'}]

in sqlite a string in double quotes i not a literal but an identifier.
so "year" must be 'year'. is this a bug in the backend?

konstantin

On Oct 2, 8:16 am, akonsu <ako...@gmail.com> wrote:
> hello,
>
> this works with sqlite:
>
> AppModel.objects.extra(select={'year':"django_date_trunc('year', \"date
> \")"}).values('year').order_by().annotate(total = Count('id'))
>
> i assume that for postgresql i would have to use date_trunc function.
>
> is there a portable way to do this?
>
> thanks
> konstantin
--~--~---------~--~----~------------~-------~--~----~
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