odoo里面context用法


原文轉自:https://www.cnblogs.com/zhaoweihang/p/9698852.html

 

 

<field name="partner_id" string="Customer"
widget="res_partner_many2one"
context="{'default_name': contact_name, 'default_street': street, 'default_city': city, 'default_state_id': state_id, 'default_zip': zip, 'default_country_id': country_id, 'default_function': function, 'default_phone': phone, 'default_mobile': mobile, 'default_email': email_from, 'default_user_id': user_id, 'default_team_id': team_id, 'default_website': website, 'show_vat': True}"
groups="base.group_no_one"/>

context   這是一個上下文,運用很靈活  得到整個context   self.context_get()

  self.env['res.users'].context_get() 得到context里面對應的值 得到flag的值     self.env.context.get('flag',False)

修改context里面的對應的值   self.with_context({'flag': False})   或   self.with_context(flag=True) 單一時   或   ctx = dict(context or {})   ctx['flag']=False   self.with_context(ctx)

保證context的字典類型   context = dict(context or {})

復制context   c = context.copy()   主要改變部分值,帶到其它函數中

自己構造context   context = {'flag':Fasle,'lang':'zh_cn'}

常規用法  

帶入函數中    

if part.lang:       

      self = self.with_context(lang=part.lang)     

    product = self.env['product.product'].browse(product)

   ------   

   return self.with_context(ctx).write({'invoice_line': []})   

記住,用了 with_context

視圖中引入

context   model="ir.actions.act_window"時   {"search_default_my_export_list":1}  

代表 搜索時 my_expert_list 值為1 對於 search_default_是系統的前置標識

 

設置值:獲取表單中的字段:【字段1、字段2】

     self.with_context({'字段1': 'aaaaaa','字段2': 'bbbaaa'})

 

獲取值:

self._context.get('字段1')

案例00001:

@api.multi
def _compute_price_rule(self, products_qty_partner, date=False, uom_id=False):
""" Low-level method - Mono pricelist, multi products
Returns: dict{product_id: (price, suitable_rule) for the given pricelist}

If date in context: Date of the pricelist (%Y-%m-%d)

:param products_qty_partner: list of typles products, quantity, partner
:param datetime date: validity date
:param ID uom_id: intermediate unit of measure
"""
self.ensure_one()
if not date:
date = self._context.get('date') or fields.Date.context_today(self)
if not uom_id and
self._context.get('uom'):
uom_id = self._context['uom']
if uom_id:
# rebrowse with uom if given
products = [item[0].with_context(uom=uom_id) for item in products_qty_partner]
products_qty_partner = [(products[index], data_struct[1], data_struct[2]) for index, data_struct in enumerate(products_qty_partner)]
else:
products = [item[0] for item in products_qty_partner]

if not products:
return {}

 

 

 

 

案例00002


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM