Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django Queryset with year(date) = '2010'

I'm trying to build this query

select * from m_orders where year(order_date) = '2010' 

the field order_date is a DateTime field. I just don't want to use raw sql queries here. Is it even possible to use e.g. MySQL functions in django quersets?

like image 504
onigunn Avatar asked Aug 15 '10 13:08

onigunn


2 Answers

You can achieve this without using raw SQL. Use the built in __ mechanism instead (see the documentation for more details). Something like this:

MyOrder.objects.filter(order_date__year = 2010)
like image 110
Manoj Govindan Avatar answered Oct 13 '22 20:10

Manoj Govindan


you can use django's builtin query API for this. no need for any vendor specific code or raw SQL.

it would probably look something like this:

Orders.objects.filter(order_date__year=2010)

http://docs.djangoproject.com/en/dev/topics/db/queries/

like image 34
ozk Avatar answered Oct 13 '22 20:10

ozk