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

I have defined my function like this:

    @api.multi
    def name_get(self):
        retvals = []     
        for record in self:
            if context.get('prod_form', False):
                catstring =  "[" + record.name + "] " + record.description
                retvals.append((record.id,catstring))
            else:
                retvals.append((record.id,record.name))
       
        return retvals

I've tried to use "context" by itself, as "self.context", "context.get()", etc.  I get errors all around.  How do I test to see if a context is set, if I use "self" as the only parameter?  Can I just say "def name_get(self, context=None)" ?

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

Hello Jeff, good question.

As the new API guidelines state, 'cr', 'uid' and 'context' are encapsulated in the new 'Environment' object.

Property attributes are available in the 'Environment' object for 'user' ('self.env.user') and 'lang' ('self.env.lang').

Context values can be retrieved directly as 'self.env.context.get('value_key', value_if_undefined)',  sample usage at 'res.partner'

https://github.com/odoo/odoo/blob/8.0/openerp/addons/base/res/res_partner.py#L307

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

Thanks, Marvin! I had read over that part, but it didn't register in my brain. Your example of 'self.env.context.get()' made it click. :)

Related Posts ตอบกลับ มุมมอง กิจกรรม
Error in new API name_get function? แก้ไขแล้ว
1
ส.ค. 15
6751
1
ก.ย. 23
2290
3
เม.ย. 19
3236
0
มี.ค. 15
3664
5
พ.ค. 25
33785