跳至內容
選單
此問題已被標幟
2 回覆
1426 瀏覽次數

I created a custom app to add some fields to the project module (see code below).
I can succesfully load the app but when I open the project task I get the following error. How to resolve this?


-------- error -----------
AttributeError: 'project.task' object has no attribute '_compute_progress'

The above server error caused the following client error:

null


-------- code -----------

from odoo import fields, models, api


@api.depends("planned_effort", "realized", "kanban_state")

def _compute_progress(self):

    for record in self: 

        if record['kanban_state'] == 'done':

            record['progressed'] = 100

        elif record['planned_effort'] > 0: 

            record['progressed'] = round(record['realized'] / record['planned_effort'] * 100)

        else:

            record['progressed'] = 0


class ClassAdditionalFields(models.Model):

   _inherit = 'project.task'

   planned_effort = fields.Integer(string='Planned effort (hr)', default=1)

   realized = fields.Integer(string='Realized (hr)', default=0)

   progressed = fields.Integer(string='Task progress', compute="_compute_progress", readonly=True)

 

頭像
捨棄
最佳答案

Hi,

This is because you have defined the compute function outside of your class definition as global. Try to define your function under the class definition (see the code below) , this will solve this issue

class ClassAdditionalFields(models.Model):
_inherit = 'project.task'

planned_effort = fields.Integer(string='Planned effort (hr)', default=1)
realized = fields.Integer(string='Realized (hr)', default=0)
progressed = fields.Integer(string='Task progress', compute="_compute_progress", readonly=True)

@api.depends("planned_effort", "realized", "kanban_state")
def _compute_progress(self):
for record in self:
if record['kanban_state'] == 'done':
record['progressed'] = 100
elif record['planned_effort'] > 0:
record['progressed'] = round(record['realized'] / record['planned_effort'] * 100)
else:
record['progressed'] = 0

Hope this will help you

Thanks

頭像
捨棄
作者 最佳答案

Hi,

It worked! Thank you. 
Had the @api before under the class definition but forgot to have the same indent as the fields above. 
This indent together with placing the @api under the class definition made the difference.

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
3
8月 25
2836
1
5月 25
2776
1
4月 25
3742
1
4月 25
4612
1
4月 25
2045