Odoo Help

Welcome!

This community 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.

0

ORM Search Method Date Condition

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

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