SQL query in Odoo

 

 

Sql query in Odoo model

@api.multi
def get_all_so(self, name=None):
    sql = "select * from sale_order where name like '%s%' order by name desc;" % name
    self.env.cr.execute(sql)
    res_all = self.env.cr.fetchall()
    #fetchall() will return an array of dictionaries
    return res_all

@api.multi
def get_so(self, name=None):
    sql = "select * from sale_order where name like '%s' order by name desc;" % name
    self.env.cr.execute(sql)
    #fetchone() will return the first element found as dictionary
    res_one = self.env.cr.fetchone()
    return res_one

print(self.get_all_so(name='SO2018'))
[{'name':'SO2018001','partner_id':'Customer 1',...},{'name':'SO2018002','partner_id':'Customer 2', ...}]

print(self.get_so(name='SO2018001'))
{'name':'SO2018001','partner_id':'Customer 1',...}

2 thoughts on “SQL query in Odoo

  1. ISLAM_GHOST Reply

    i’ve this error when i try it

    NameError: name ‘self’ is not defined

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.