跳至内容
菜单
此问题已终结
6 回复
13706 查看

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.

形象
丢弃
最佳答案

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.


形象
丢弃
最佳答案

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

形象
丢弃
编写者

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

编写者

ok Niyas

Worked like charm ! Thanks Niyas.

相关帖文 回复 查看 活动
1
1月 25
1589
2
9月 22
9356
2
4月 22
4367
0
7月 21
7027
1
3月 21
4746