Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
5 ตอบกลับ
5678 มุมมอง

I have defined this functional field so that I can get values from one2many field and display them in tree view:

    def get_product_brands(self, cr, uid, ids, fields, arg, context):

        context = context or {}

        res={}

        for record in self.browse(cr, uid, ids, context=None):

            brands = ''

            for val in record.application_data_product_template_ids

                if val.brand.name:

                    brands += "%s " % (val.brand, )

                print brands

            res[record.id] = brands and brands[:-1] or ""

        return res


my field defintion:

'brands' : fields.function(get_product_brands, method=True, string="Product brands", type='char')


But I'm getting this error:


File "/home/odoo/addons/bal_dev/balerce_dev.py", line 209, in get_product_brands_2

for record in self.browse(cr, uid, ids, context=None).application_data_product_template_ids:

File "/home/odoo/openerp/fields.py", line 755, in __get__

record.ensure_one()

File "/home/odoo/openerp/models.py", line 5219, in ensure_one

raise except_orm("ValueError", "Expected singleton: %s" % self)

except_orm: ('ValueError', 'Expected singleton: product.template(8, 4, 5, 9, 2, 3, 7)')

อวตาร
ละทิ้ง

application_data_product_template_ids is your field? in this model?

ผู้เขียน

application_data_product_template_ids, is a new one2many field in product.template model. I use this field to store brand values. What this function needs to do is to fetch all brand values stored in this new one2many field

คำตอบที่ดีที่สุด

Hi,

You can't use '.' operator here,

self.browse(cr, uid, ids, context=None).application_data_product_template_ids: 

because self.browse(cr, uid, ids, context=None)  contains more than one record.

you can iterate that or make sure it will receive single record

That's why you are getting -> 'ValueError', 'Expected singleton

Hope this helps....


อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

I modify your code in your post, should work, bye

อวตาร
ละทิ้ง
ผู้เขียน

if val.brand.name: brands += "%s " % (val.brand.name, ) words = brands.split() new_brands = ' '.join(sorted(set(words), key=words.index)) Added this, so that I can eliminate reapeted values....thanks!!

Related Posts ตอบกลับ มุมมอง กิจกรรม
4
ธ.ค. 23
17892
1
ก.ย. 16
7561
0
มี.ค. 15
3791
0
ก.ย. 24
296
1
มี.ค. 23
2456