Odoo - Give a warning when editing some field with empty value

Viewed 1435

I want to add an entry to the suppliers table if one creates a new or edits a product. Each product must have a supplier. If a supplier is not selected, system must give a warning “You should fill in the supplier details, at least one.”

Here is my code:

class warning_supplier(models.Model):
_inherit = 'product.template'

@api.multi
def write(self, vals):
    res = super(warning_supplier, self).write(vals)
    supplier_id = self.env['res.partner'].search([('name','=','No Supplier')])
    for this in self:
        seller_ids = this.seller_ids
        if len(seller_ids)==0:
            raise Warning('You should fill in the supplier details, at least one.')
    return res

When I create products, the code runs correctly.

But when I edit a product & remove the selected supplier, it does not work any more.

Can someone pinpoint the error to me? Thanks!


Edit: Fixed by using constraints.

2 Answers
Related