odoo里面context用法

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

 

 

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

你可能感兴趣的:(odoo里面context用法)