如何解决如何获得在odoo中特定领域的学生的名字?
我有一个学生表格,当学生选择他/她的出生日期时,他的年龄将显示在“计算出的年龄”字段中。
现在,我希望不仅显示数字,而是显示为:
嗨,Srishti!你的年龄是9岁。
请注意,“ Srishti”是在名称字段中给出的名称。
请帮助。
致谢
Adesh Paul
class StudentStudent(models.Model):
_name = 'student.student'
student_dob = fields.Date(string="Date of Birth")
age = fields.Integer(string='Age')
calc_age = fields.Char(string="Calculated Age",readonly=True)
@api.onchange('student_dob')
def _onchange_birth_date(self):
"""Updates age field when birth_date is changed"""
if self.student_dob:
d1 = datetime.strptime(str(self.student_dob),"%Y-%m-%d").date()
d2 = date.today()
self.calc_age = relativedelta(d2,d1).years
解决方法
您可以将age
和calc_age
定义为计算字段,而不用使用onchange
方法来设置它们的值。
您可以使用相同的计算方法,因为两个字段都依赖于同一字段student_dob
。
示例:
student_dob = fields.Date(string="Date of Birth")
age = fields.Integer(string='Age',compute="_compute_age")
calc_age = fields.Char(string="Calculated Age",compute="_compute_age")
@api.depends('student_dob')
def _compute_age(self):
for record in self:
if record.student_dob:
age = relativedelta(
fields.Date.today(),record.student_dob,).years
record.age = age
record.calc_age = 'Hi,{}! Your age is {}'.format(record.name,age)
从relativedelta
导入dateutil.relativedelta
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。