This question has been flagged
1 Reply
13820 Views

I have a date field in this format: "17/01/2013" and I'm trying to use the ORM search function to find all records that match a certain year say 2012 for instance.

I've tried something like this but it didnt work:

my_obj.search(cr, uid, [('date', '>=', '01/01/' + h1.year), ('date', '<=', '31/12/' + h1.year)], order='date')

but it didn't work.

How can I achieve this with the ORM search function? More general, how can I construct a query to search for all records matching a specified year using a date field in the format shown above?

Avatar
Discard
Best Answer

Hi. If you do not have any exact error message, just the result is NULL, may be your date format isn't good. You can check the system date format by print. Ex.

   for obj in my_obj.browse(cr,uid,[some_id]): 
    print obj.date

You can see the result, and you can modify your search condition according the result. Any others, please give more info's.

This is a date format.:

date_planned = datetime.strptime(order.shipping_date, DEFAULT_SERVER_DATE_FORMAT)

please see that: for datetime: http://docs.python.org/2/library/datetime.html

and you can find a lot of relevant info's there: http://stackoverflow.com/questions/466345/converting-string-into-datetime

Avatar
Discard
Author

Thanks for your response. I confirmed the date format as recommended and it showed as %Y-%m-%d i.e. 2013-12-31 so I tried this syntax:

Author

I confirmed the date format as recommended to be 2013-12-31 so I used the syntax: my_obj.search(cr, uid, [('date', '>=', h1.year + '-01-01'), ('date', '<=', h1.year + '-12-31')], order='date') but it still doesn't return any results. I tested this syntax: ('date', '=', '10/12/2013') to test and I got a result. What do you think? I edited the question a bit too.

May the problem with the type of the variable. h1.year is sting but you need date. This is a python typecasting issue. I refresh my post, pls see it.

Author

Thanks very much Klacus. It was a date format issue. I eventually got the right date format and used the strptime function to convert it. Its working now.

Your welcome!