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

company_id = filed.Many2one('res.company','Company', required=True)

I defined a field like this. When I open a form, I must fill this company_id to complete save.Unfortunatly, when i use import function, the company_id is not necessary anymore. I can finish validate without company_id.

So , how can I make this field checked when I use import function?

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

Constraints are actually evaluated only when a modification on that field is done. when your field is required param set, you cant save the form until it is filled. You should define the company id on the desired form first.

Another option is, override the orm function write() and check the dict contain the value company_id, if not raise the error.

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

I find a way to work. But it's not perfect.Like this:

Would you please give me some advise.tks

@api.constrains('license_plate')
def _validate_plate(self):    
    res = self.env['fleet.vehicle'].sudo().search([('license_plate', '=', self.license_plate)])    
    if len(res) > 1:        
        raise ValidationError('This license plate already exist')    
    else:                
        if not res.company_id:                        
            raise ValidationError('You must choose company!')

Why I can't use below code? In fact, it's never execute for some reason, which i don't know.

@api.constrains('company_id')
def _check_company(self):    
    if not self.company_id:        
        raise ValidationError('You must choose company!')
Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 9 23
1996
4
thg 4 25
1018
0
thg 4 25
387
3
thg 2 23
4230
0
thg 10 22
1628