Odoo Help

1

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

By
Tatorus
on 3/10/16, 6:17 AM 1,885 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')

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

1 follower(s)

Stats

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