Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
6 Trả lời
13718 Lượt xem

I want to restrict the duplicate value in one2many field ,if it is there then it should not add in one2many field and it should raise error.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Dear,

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.


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

Using the sql constrains, you can do this.


Take the case of sale order and sale order line, the field order_id in the sale order line is the Many2one field between these two model.


Suppose if you don't want to add duplicate products in order line, ie, apple(a product) can be selected once in a order line of a sale order, you can add the constrains like this,


_sql_constraints = [('order_product_uniq', 'unique (order_id,product_id)',     
'Duplicate products in order line not allowed !')]


Thanks

Ảnh đại diện
Huỷ bỏ
Tác giả

Thanks Niyas....But it is not working for me...i tried ..actually i added variants in new tab which has two field attribute and attribute values like product variants is there in product. In product variants is also adding duplicate attribute and as well as in sale order also ...other idea can u suggest me.

You can check the case in a new db, sql constrains will work with new database

Tác giả

ok Niyas

Worked like charm ! Thanks Niyas.

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 1 25
1595
2
thg 9 22
9365
2
thg 4 22
4374
0
thg 7 21
7038
1
thg 3 21
4755