This question has been flagged
2 Replies
4820 Views

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?

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author Best Answer

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!')
Avatar
Discard