Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
5950 Vistas

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
Descartar
Mejor respuesta

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
Descartar
Autor Mejor respuesta

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
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
sept 23
1715
4
abr 25
586
0
abr 25
200
3
feb 23
3742
0
oct 22
1474