Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
1557 มุมมอง

it is very first attempt to use SQL within Odoo

i have created a View (employee_tenure) in database and want to call in a compute method to store in database the tenure of a employee, i run this view on pgAdmin and its working fine. 

please help to correct the Compute method, is this correct or what is the correct way to achieve this?


My View in databse

create or replace view employee_tenure as 
select e.id, e.name, c.date_start,  
       current_date - c.date_start as tenure_days, 
       (current_date - c.date_start) / 30 as tenure_months,
       ((current_date - c.date_start) / 30) / 12 as tenure_years
  from hr_employee e
  left join hr_contract c on e.contract_id = c.id;


My Compute method in Technical -> Models -> Employee:

for rec in self:
    query = """select id, name, tenure_days, tenure_months, tenure_years from employee_tenure where id =%s)""" %(rec.id)
    # 
    self.env.cr.execute(query)
    result = self.env.cr.dictfetchall()
    # 
    for row in result:
        rec['x_emp_tenure_months'] = row.get(tenure_months)


it is showing error:

psycopg2.errors.SyntaxError: syntax error at or near ")"
LINE 1: ...nths, tenure_years from employee_tenure where id =NewId_448)

why is NewId_448 instead of 448? how to get rid of this problem?

i have used rec.id and it is an already existing record, i am updating it.


regards


อวตาร
ละทิ้ง
ผู้เขียน

can somebody help for it...

for rec in self:
query = """select id, name, tenure_days, tenure_months, tenure_years from employee_tenure where id =%s)""" %(rec.id)
#
self.env.cr.execute(query)
result = self.env.cr.dictfetchall()
#
for row in result:
rec['x_emp_tenure_months'] = row.get(tenure_months)

it is showing error:

psycopg2.errors.SyntaxError: syntax error at or near ")"

LINE 1: ...nths, tenure_years from employee_tenure where id =NewId_448)

why is NewId_448 instead of 448? how to get rid of this problem?

i have used rec.id and it is an already existing record, i am updating it.

คำตอบที่ดีที่สุด

Hi,

Instead of using result.tenure_months you can use result['tenure_months'] or result.get('tenure_months') .You can use the following code 

def _compute_field():
for rec in self:
query = """select id, name, tenure_days, tenure_months, tenure_years from employee_tenure where id =%s)""" %(rec.id)
self.env.cr.execute(query)
result = self.env.cr.dictfetchall()
if result['tenure_months'] > 12:
tenure_in_days = result['tenure_days']
tenure_in_months = result['tenure_months']
tenure_in_years = result['tenure_years']
rec.x_emp_tenure_days = tenure_in_days
rec.x_emp_tenure_months = tenure_in_months
rec.x_emp_tenure_years = tenure_in_years
rec.is_eligible = True
else:
rec.is_eligible = False


Regards


อวตาร
ละทิ้ง
ผู้เขียน

it is showing error:

psycopg2.errors.SyntaxError: syntax error at or near ")"
LINE 1: ...nths, tenure_years from employee_tenure where id =NewId_448)

why is NewId_448 instead of 448? how to get rid of this problem?

Related Posts ตอบกลับ มุมมอง กิจกรรม
Set default value for Html field!? แก้ไขแล้ว
5
พ.ค. 24
13154
4
ก.พ. 24
2419
0
ส.ค. 21
111
odoo9: why default value does not work แก้ไขแล้ว
7
ก.ค. 16
5675
1
มี.ค. 25
487