Odoo Help

0

ORM Search Method Date Condition

By
Ademola Oke
on 1/21/14, 9:43 PM 2,871 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?

1
klacus
On 1/21/14, 11:22 PM

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

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:

Bam Technologies Limited, Ademola Oke
on 1/22/14, 9:46 AM

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.

Bam Technologies Limited, Ademola Oke
on 1/22/14, 9:49 AM

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.

klacus
on 1/22/14, 10:59 AM

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.

Bam Technologies Limited, Ademola Oke
on 1/23/14, 4:14 PM

Your welcome!

klacus
on 1/26/14, 12:43 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)

Stats

Asked: 1/21/14, 9:43 PM
Seen: 2871 times
Last updated: 3/16/15, 8:10 AM