Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
1941 Lượt xem
I made a new model to show the sale price based on fixed margin (see code below)
it worked and displayed on the product view normally,

class Corporate(models.Model):
_inherit = 'product.template'

Marge = fields.Float(string="Marge")
Price = fields.Float(string="Price", compute='compute_Price')

def compute_Price(self):
self.Price = self.Marge * self.standard_price
return self.Price
WHen i added the field to product.template tree view, i got this error:

During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Program Files\odoo\server\odoo\models.py", line 5119, in ensure_one _id, = self._ids ValueError: too many values to unpack (expected 1)


 File "C:\Program Files\odoo\server\odoo\models.py", line 5122, in ensure_one
    raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: product.template(202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 222, 223, 224, 225, 226, 227, 228, 229, 230, 232, 231, 233, 234, 221, 235, 236, 237, 248, 246, 238, 254, 239, 247, 244, 255, 245)


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

Hi,

The error message shows which is singleton error. The singleton is occuring because there is more than one record.To avoid the single to error you can use a loop on the beginning of the function

def compute_Price(self):

    for rec in self:

        rec.Price = rec.Marge * rec.standard_price

        return rec.Price


Hope it helps



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

Thanks it was helpfull

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

Most If the time you see an expect singleton error it’s due to the code only being able to handle one computation at a time. Change it so it can handle multiple records at a time.

def compute_Price(self):
for template in self:
template.Price = template.Marge * template.standard_price

self in this case is the entire product.template model not just a single record. So you can loop through each one with a “for loop”. Pick whatever you want for template, could be “purple_unicorn” so long as it’s consistent in the function.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 3 15
4024
1
thg 4 24
2104
2
thg 3 24
3295
2
thg 10 18
8598
4
thg 9 16
3326