odoo14 context上下文的用法及使用场景

我们在写程序的时候经常会遇到这样的场景:

  1. 跳转视图的时候想要把当前视图的数据同时传递过去,在另一个视图直接使用
  2. 定义one2many字段时,把当前的数据传递到one2many字段的表直接使用
  3. 定义一个向导,将当前页面的数据传递到向导页面
  4. 跳转视图界面时进行默认搜索显示

此时我们就需要用到contex上下文了。context是一个字典

context={'字段名1':'字段值1','字段名2':'字段值2',}

1.获取context的方式:

  • self._context         获取整个context
  • self.env.context     获取整个context
  • self._context.get('字段名')    获取context中指定字段的值
  • self.env.context.get('字段名')    获取context中指定字段的值

2.设置修改context的值

保证context的字典类型   context = dict(context or {})

设置传递context的值   self.with_context({'字段名': '值'})

复制context的值     c = context.copy()

3.传递context的方式:

  • 例如:按钮跳转视图时将当前模块记录的id传递过去,在其他视图界面通过接收值进行操作
<button name="%(purchase_management.act_window_purchase_application_wizard)d" string="退回修改" type="action" states="1_approval" class="oe_highlight"
 context="{'model_id': active_id}"/>
  • 菜单跳转视图界面时进行默认搜索显示(默认视图界面分组显示)
    <record id="work_plan_act_window" model="ir.actions.act_window">
        <field name="name">工作计划</field>
        <field name="res_model">work.plan</field>
<!--     设置默认分组-->
        <field name="context">{'group_by': 'date:day'}</field>
        <field name="search_view_id" ref="work_plan_search_view"/>
        <field name="view_mode">tree,form,search</field>
    </record>
  •  按钮跳转视图界面时默认搜索显示(跳转到新界面后显示name为活动字段id的记录)
<button name="%(supplier_management.act_window_purchase_record_view)d" class="oe_stat_button"
                                icon="fa-th-list" type="action" groups="supplier_management.group_manager"
                                context="{'search_default_name': active_id, 'default_name': active_id, 'group_expand': True}"/>
  • 给 one2many字段传递值
<field name="purchase_record_line" context="{'id': active_id, 'product_id': product_id, 'attribute': attribute}">
                            <tree editable="bottom">
                                <field name="order_id"/>
                                <field name="name"/>
                                <field name="category_id" invisible="1"/>
                                <field name="product_id" invisible="1"/>
                                <field name="attribute" invisible="1"/>
                                <field name="purchaser_id" invisible="1"/>
                                <field name="date"/>
                                <field name="quantity"/>
                                <field name="price" widget="monetary" options="{'currency_field': 'currency_id'}"/>
                                <field name="rate"/>
                                <field name="currency_id" invisible="1"/>
                                <field name="subtotal_tax" string="小计税额" invisible="1"/>
                                <field name="subtotal" string="小计" widget="monetary" options="{'currency_field': 'currency_id'}"/>
                            </tree>
</field>

使用 self._context.get('context_key_name') 获取传递的值

    @api.onchange('name')
    def _onchange_name(self):
        # 根据供应商数据设置供应商分类默认值
        for res in self.name:
            if res.id:
                self.product_id = self._context.get('product_id')
                self.attribute = self._context.get('attribute')
                self.order_id = self._context.get('id')
  •  在self的记录集传递上下文(显示active=False的记录)
vals = self.env['supplier.management'].with_context(active_test=False).search([('id', '=', item)])

odoo上下文的用途非常广泛,这里我只简单的举了几个例子。相信理解了的话在其他场景也会很容易就能使用

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Odoo 14的数据字典是一种用于定义和描述数据库表结构、字段属性和关系的工具。在Odoo开发中,数据字典是非常重要的,它提供了一个清晰的方式来组织和管理数据库模型。 数据字典中的主要元素包括表(模型)、字段、关系和约束。 表(模型)是数据字典的基本单元,它代表着一个数据库表。每个模型都有一个唯一的标识符和一个可选的表名。模型可以包含字段和关系,用于定义表的结构和属性。 字段是模型中的属性,用于存储和处理数据。字段可以是不同的类型,如字符、整数、日期、关联等。每个字段都有一个唯一的标识符和一个可选的字段名。字段还可以具有一些属性,如必填、只读、唯一等。 关系用于在模型之间建立联系。在Odoo中,主要有三种类型的关系:一对一关系、一对多关系和多对多关系。关系可以通过字段定义,其中涉及到一个字段类型为Many2one、One2many或Many2many。 约束用于限制字段值的范围或特性。在Odoo中,可以使用不同的约束类型,如唯一约束、非空约束和长度约束。约束可以直接在字段定义中添加,以确保数据的完整性和一致性。 通过使用Odoo 14的数据字典,开发人员可以清晰地定义和管理数据库模型,为应用程序提供可靠和高效的数据结构。此外,数据字典还支持自动化的数据库表创建和更新过程,使系统维护更加简单和方便。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值