Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
6184 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit
Autor Nejlepší odpověď

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
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
1
zář 23
1984
4
dub 25
997
0
dub 25
375
3
úno 23
4199
0
říj 22
1624