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.

1

How to compare a string and a datetime in a search domain ?

By
Tatorus
on 3/10/16, 6:17 AM 918 views

Hi, i have to select in idProd the products who respect those conditions :

idProd = product_product.search([('product_tmpl_id.ing', '=', True), ('product_tmpl_id.last_synchro_ing', '<', date)], order='id')


The problem is for the sample in bold because "product_tmpl_id.last_synchro_ing" is a string and "date" is a datetime.

So I don't know how to compare them in a search domain, thanks in advance.

1
Motez
On 3/11/16, 6:38 AM

You need to convert your datetime object to a string so postgre can handle it. use the strftime method

idProd = product_product.search([('product_tmpl_id.ing', '=', True),
('product_tmpl_id.last_synchro_ing', '<', date.strftime("%Y-%m-%d"))
], order='id')

0
Damien Bouvy (dbo)
On 3/16/16, 6:16 PM

Hi,

There are also helper functions on the fields.Date model:

 
def from_string(value):
     """ Convert an ORM ``value`` into a :class:`date` value. """
def to_string(value):
     """ Convert a :class:`date` value into the format expected by the ORM. """

So you could do

idProd = product_product.search([('product_tmpl_id.ing', '=', True), ('product_tmpl_id.last_synchro_ing', '<', fields.Date.to_string(date))], order='id')

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

1 follower(s)

Stats

Asked: 3/10/16, 6:17 AM
Seen: 918 times
Last updated: 4/19/16, 4:24 PM