跳至內容
選單
此問題已被標幟
4 回覆
3632 瀏覽次數

Hello,

In below code, product_id is my Many2many field of product1 table and price is a field of that table When i select more than one product in Many2many field, I get this error : ValueError: Expected singleton:
product1.product1(2, 4)  . can anyone help me.

Thanks in advance.


Code:


total = fields.Float("Total",readonly=True,compute="count")

product_id = fields.Many2many('product1.product1',string="Products")

@api.onchange('product_id')
def count(self):
for i in self:
i.total = i.product_id.price

頭像
捨棄

Thanks, Mitul Singala it works.




最佳答案

Hello,

in your code product_id is many2many field. and into method line: i.product_id.price, you get the multiple product ids. that's why you get singleton error.make below change into your code:

@api.onchange('product_id')
def count(self):
    for i in self:
        total = 0
        for prod in i.product_id:
            total += prod.price
        i.total = total
頭像
捨棄
最佳答案

Hi Ruturaj,

ValueError: Expected singleton: product1.product1(2, 4)  it means product_id return multiple records you can not directly assign values.

Example:

@api.onchange('product_id')
def count(self):
    for i in self:
        total = 0
        for p in i.product_id:
            total += p.price
        i.total = total
Thank You.
頭像
捨棄
作者 最佳答案


頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
4
3月 24
1604
4
12月 19
5282
2
7月 23
2333
1
12月 20
14458
5
2月 20
9038