(49) odoo context操作


* context
  這是一個上下文,運用很靈活

* 得到整個context
  V7
  context=dict(context or {})
  這個版本是明傳
  V8
  self.context_get()
  self.env['res.users'].context_get()

* 得到context里面對應的值

  得到flag的值
 
  V8
  self.env.context.get('flag',False)

  V7
  context.get('flag',False)

* 修改context里面的對應的值

  V8
  self.with_context({'flag': False})
  或
  self.with_context(flag=True) 單一時
  或
  ctx = dict(context or {})
  ctx['flag']=False
  self.with_context(ctx)

  v7
  context = dict(context or {})
  這句不在改前一定要有,否則報錯
  -------
  context.update({'flag': False})
  或:
  context.update(flag=True)單一時
  或:
  context['flag']=False

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

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

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

* 常規用法
  v7 帶入函數中
      def create(self, cr, uid, vals, context=None):
        context = dict(context or {})

  v8 帶入函數中
    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"時
  <field name="context">{"search_default_my_export_list":1} </field>
  代表 搜索時 my_expert_list 值為1 對於 search_default_是系統的前置標識

  分組
  <filter string="Day"name="group_day"context="{'group_by':'date_start:day'}"

  指定搜索
  <field name="department_id" string="Department" context="{'invisible_department': False}"/>


  列表中字段
  <field name="line_ids" context="{'currency_id': currency_id,
   'default_analytic_account': context.get('analytic_account', '')}">

視圖定義context 帶入函數
<field name="product_id" on_change="onchange_product_id(product_id, context)"
  context="{'default_hr_expense_ok':1}"/>

* context上面是常用的方法,掌握后,就可以在整個系統中方便傳數值,但是上下文,不要傳太多                               


免責聲明!

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



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