Skip to Content
Menu
This question has been flagged
4 Replies
4141 Views

Hello,

Previously in the purchase order more than 100 products are entered, many times the user is equipment and orders twice the same product, how can I solve it?

Something that blocks me if they repeat themselves or give me a warning?

Avatar
Discard
Best Answer

Hi,

You can use sql constrains for this,

_sql_constraints = [
    ('Unique product', 'unique(order_id,product_id)', 'duplicate order line exists'),
    ]

Thanks

Avatar
Discard
Author

Hello, thanks for the answer. this line in which part of the code should I add? Thank you.

Best Answer

Dear Eduardo,

To check exist the same Product in the line level.

from odoo import models, fields, api
from odoo.exceptions import ValidationError


class PurchaseOrder(models.Model):
_inherit = "purchase.order"

@api.multi
@api.constrains('order_line')
def _check_exist_product_in_line(self):
for purchase in self:
exist_product_list = []
for line in purchase.order_line:
if line.product_id.id in exist_product_list:
raise ValidationError(_('Product should be one per line.'))
exist_product_list.append(line.product_id.id)

Hope this code help you

Best Thanks,

Ankit H Gandhi.


Avatar
Discard
Author Best Answer

Hello, thanks for the answer. this line in which part of the code should I add? Thank you.

Avatar
Discard
Related Posts Replies Views Activity
2
Jan 24
816
0
Nov 23
449
0
Sep 23
327
2
Aug 23
1792
2
Aug 21
4316