Skip to Content
Menu
This question has been flagged
2 Replies
7079 Views

hello everybody , I  want to add a a constraint but I don't have the idea to do it . here is my code 

so l have tow type of products(article) TTC and UA so how I can change  the order-line to stop the user entering tow type of product in the same quotation , we should not have tow different type of product TTC and UA in tha same quotation

thank you in advance .  

class Article(models.Model):
_inherit = 'product.template'
_name = 'product.template'
Reference_article = fields.Char('Internal Reference', index=True, required=True)
purchase_ok = fields.Boolean(default=False)
quantite = fields.Integer()
prix_tarif = fields.Float(digits=(8, 2), required=True)
prix_net = fields.Float(digits=(8, 2), required=True)
typea = fields.Selection([('t', 'TTC'), ('u', 'UA')], default='t', required=True)
emplacment_ev = fields.Many2one('euler.emplacement', string="Emplacement")
class Sales(models.Model):
_inherit = 'sale.order'
_name = 'sale.order'


Avatar
Discard
Best Answer

Maybe the simplest way is to use @api.constrains. It does a python-based custom validation for a field every time a record is updated. You could write the validation function like this:

from openerp import api, exceptions

class Sales(models.Model):

    _inherit = 'sale.order

    @api.constrains('order_line')
    def check_product_type(self):
        for sale_order in self:
            # Check how many different product types exist on sale order lines
            used_types = set([line.product_id.typea for line in sale_order.order_line])
            if len(used_types) > 1:
                raise exceptions.ValidationError('Only one product type should be used')


Alternatively, you could also use @api.onchange to show an error message immediately after the user adds a new sale order line with a wrong product type.


Avatar
Discard
Author Best Answer
Thanks for your answer Mr Timo , but I don't know why your solution don't want to work , but it give my an idea , in my model I use this @api.onchange('order_line')
def _onchange_price(self):
if len(self.order_line) > 0:
refr = self.order_line[0].product_id.typea
for record in self.order_line:
if record.product_id.typea != refr:
return {
'warning': {
'title': "Erreur de type article",
'message': "veuillez choisir des articles de meme type!"
}
}

 it show the message but it show it tree time I don't know what I can do to solve this !



Avatar
Discard

What kind of error did you get with the @api.constrains solution?