Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
2998 Lượt xem

I'm trying to add date of birth field in res.partner  and the date must be in the past

this is the code:

from odoo import models , fields,api,_
from odoo.exceptions import ValidationError
from odoo.exceptions import UserError

class ResPartner(models.Model):
_inherit='res.partner'
is_student=fields.Boolean(string="Is Student?")
birth_date=fields.Date(string="Birth Date",rquired=True)


@api.constrains('birth_date')
def validation_constraints(self):
today=fields.Date.today()
for rec in self:

if rec.birth_date>=today:
raise ValidationError(_('Invalid date of birth ..please enter correct date'))

  

but when i create the user without insert birth date the error occurs:
'>=' not supported between instances of 'bool' and 'datetime.date'


How can i solve this problem when the user doesn't insert the date?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

Please update the if condition as follows:


if rec.birth_date and rec.birth_date >=today:


Thanks & Regards

Walnut Software Solution

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 7 23
3649
4
thg 6 25
8949
2
thg 10 24
7800
2
thg 9 24
5224
2
thg 3 24
3400