Odoo is the world's easiest all-in-one management software. It includes hundreds of business apps:
CRM | e-Commerce | Accounting | Inventory | PoS | Project management | MRP | etc.
I am trying to retrieve a list in my database. My statement is this:
cr.execute("select es.* from student_resource es where es.date between '"+str(date_from)+"' and '"+str(date_to)+"'")
where date_from and date_to are of type fields.date but when when executed, that is i am trying to access my view, it says:
TypeError: cannot concatenate 'str' and 'int' objects
What did i miss with my statement?
As your fields are fields.date then you do not need to convert into string. Please write your query as like below.
cr.execute("select es.* from student_resource es where es.date between '" + date_from + "' and '" + date_to + "' ")
cr.execute("select es.* from student_resource es where es.date between '%s' and '%s' " %(date_from, date_to) )
Update : or
query = "select es.* from student_resource es where es.date between '%s' and '%s' " %(date_from, date_to)
When you retrieve any date/datetime value from data base into any python variable it is always in string s
o no need to convert into string.
I hope it will resole your issue.
I think you can use the to_date() in psql for that.
Try like this:
cr.execute("select es.* from student_resource es where es.date between to_date(date_from, 'YYYY/MM/DD') and to_date(date_to, 'YYYY/MM/DD')")
cr.execute("select es.* from student_resource es where es.date between to_date("+str(date_from)+", 'YYYY/MM/DD') and to_date("+str(date_to)+", 'YYYY/MM/DD') ")
Quoting Psycopg official references (see here) :
Psycopg casts Python variables to SQL literals by type. Many standard Python types are already adapted to the correct SQL representation.
Don't manually add single quotes depending of the data type, let Psycopg do that for you.
"""INSERT INTO some_table (an_int, a_date, a_string) VALUES (%s, %s, %s);""", (11, datetime.date(2007, 07, 27), "Odoo rocks!"))
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 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
|Asked: 8/18/15, 6:01 AM|
|Seen: 2703 times|
|Last updated: 8/28/15, 1:37 AM|