Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
4 Risposte
3760 Visualizzazioni

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

Avatar
Abbandona

Thanks, Mitul Singala it works.




Risposta migliore

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
Avatar
Abbandona
Risposta migliore

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.
Avatar
Abbandona
Autore Risposta migliore


Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
4
mar 24
1763
4
dic 19
5478
2
lug 23
2527
1
dic 20
14632
5
feb 20
9205