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

try to compute the days differance 


error:

raise exception.with_traceback(None) from new_cause
AttributeError: 'test.testing' object has no attribute '_compute_p_days'




py:

from datetime import date, timedelta
from odoo.exceptions import ValidationError
from odoo import api, fields, models


class test_testing(models.Model):
_name = 'test.testing'
_description = 'test code'

start_date = fields.Date(string = 'Start Date',
required = True)
end_date = fields.Date(string = 'End Date',
required = True)
p_days = fields.Float(string = 'Plan Days',
compute = '_compute_p_days',
default = lambda self: self._compute_p_days)
g_days = fields.Float(string = 'Days Gone',
compute = '_compute_g_days',
default = lambda self: self._compute_g_days)


@api.depends('start_date')
def _compute_g_days(self):
self.g_days = 0
for record in self:
if record.start_date > date.today():
raise ValidationError('Start date should not greater than end date.')
else:
record.g_days = (date.today() - record.start_date).days


@api.depends('start_date', 'end_date')
def _compute_p_days(self):
self.p_days = 0
for record in self:
if record.start_date > record.end_date:
raise ValidationError('Start date should not greater than end date.')
else:
record.p_days = (record.end_date - record.start_date).days

XML:

xml version="1.0" encoding="utf-8"?>


id="test_testing_view_form" model="ir.ui.view">
name="name">test.testing.form
name="model">test.testing
name="arch" type="xml">
string="Testing Code">

string="Information">
name="start_date" type="date"/>
name="end_date" type="date"/>
name="p_days"/>
name="g_days"/>






id="test_testing_tree" model="ir.ui.view">
name="name">test.testing.tree
name="model">test.testing
name="arch" type="xml">
string="Testing">
name="start_date" type="date"/>
name="end_date" type="date"/>
name="p_days"/>
name="g_days"/>




id="test_testing_action" model="ir.actions.act_window">
name="name">testing Code
name="type">ir.actions.act_window
name="res_model">test.testing
name="view_mode">tree,form
name="context">{}
name="domain">[]
name="help" type="html">

class="o_view_nocontent_smiling_face">
Create your first project!






id="menu_test"
name="test"
parent="test_root"
action="test_testing_action"
sequence="20"
/>




Avatar
Discard
Best Answer

Hey...


Please , try to without lambda function in field.

Avatar
Discard
Best Answer
You can make changes like below to make your code working:

p_days = fields.Float(string = 'Plan Days',
compute = '_compute_p_days',
default = 0)
g_days = fields.Float(string = 'Days Gone',
compute = '_compute_g_days',
default = 0)


@api.depends('start_date')
def _compute_g_days(self):
for record in self:
record.g_days = 0
if record.start_date > date.today():
raise ValidationError('Start date should not greater than end date.')
else:
record.g_days = (date.today() - record.start_date).days


@api.depends('start_date', 'end_date')
def _compute_p_days(self):
for record in self:
if record.start_date and record.end_date:
if record.start_date > record.end_date:
raise ValidationError('Start date should not greater than end date.')
else:
record.p_days = (record.end_date - record.start_date).days
else:
record.p_days = 0


Thanks & Regards,



CandidRoot Solutions Pvt. Ltd.

Mobile: (+91) 8849036209
Email: info@candidroot.com
Skype: live:candidroot
Web: https://www.candidroot.com
Address: 1229-1230, Iconic Shyamal, Near Shyamal Cross Road, Ahmedabad, Gujarat
Avatar
Discard
Related Posts Replies Views Activity
1
Jun 23
1299
0
Apr 24
329
2
Jul 22
4901
2
Dec 24
3113
1
Dec 24
343