Skip to Content
Menu
This question has been flagged
2 Replies
4057 Views

Hi all, 

i am using Odoo version 9

i am newbie and i search for a solution to select records from a sql query and store those selected records in fields of model in odoo.

i searched on the web and i don't find anything that pointed me on the right direction. 

Please help me on that 

thanks in advance

Avatar
Discard
Best Answer

Hi Nabil,

I think following code will help you,

class ModelName(models.model):
_name = 'my.model.name'

name = fields.Char("Name")
active = fields.Boolean("Active")

@api.multi
def get_my_records(self):
sql = """select name from my_model_name where active=True"""
self.env.cr.execute(sql)
for rec in self.env.cr.fetchall():
print("Name is", rec[0])
return True

Thanks.

Avatar
Discard
Best Answer

Hi,  

Just try like this  @api.multi

def function_name(self):
cr = self._cr
cr.execute("select field from model where condition")
temp = cr.dictfetchone()
value = temp['field']
for rec in self:
rec.field_name = value
return

field_name = fields.Char(string='STRING', compute='function_name')

Avatar
Discard