Environment

The Environment stores various contextual data(上下文数据 ) used by the ORM: the database cursor (for database queries), the current user (for access rights checking) and the current context (storing arbitrary metadata). The environment also stores caches.

All recordsets have an environment, which is immutable(不可改变的), can be accessed using env and gives access to the current user (user), the cursor (cr) or the context (context):

>>> records.env
<Environment object ...>
>>> records.env.user
res.user(3)
>>> records.env.cr
<Cursor object ...)

When creating a recordset from an other recordset, the environment is inherited. The environment can be used to get an empty recordset in an other model, and query that model:

>>> self.env['res.partner']
res.partner
>>> self.env['res.partner'].search([['is_company', '=', True], ['customer', '=', True]])
res.partner(7, 18, 12, 14, 17, 19, 8, 31, 26, 16, 13, 20, 30, 22, 29, 15, 23, 28, 74)

Altering the environment

The environment can be customized from a recordset. This returns a new version of the recordset using the altered environment.

sudo()

creates a new environment with the provided user set, uses the administrator if none is provided (to bypass access rights/rules in safe contexts), returns a copy of the recordset it is called on using the new environment:

# create partner object as administrator
env['res.partner'].sudo().create({'name': "A Partner"}) # list partners visible by the "public" user
public = env.ref('base.public_user')
env['res.partner'].sudo(public).search([])
with_context()
  1. can take a single positional parameter, which replaces the current environment's context
  2. can take any number of parameters by keyword, which are added to either the current environment's context or the context set during step 1
# look for partner, or create one with specified timezone if none is
# found
env['res.partner'].with_context(tz=a_tz).find_or_create(email_address)
with_env()
replaces the existing environment entirely

Odoo Documentation : Environment的更多相关文章

  1. Odoo Documentation : Fields

    Fields Basic fields class openerp.fields.Field(string=None, **kwargs) The field descriptor contains ...

  2. Odoo Documentation : Recordsets

    Other recordset operations Recordsets are iterable(可迭代的) so the usual Python tools are available for ...

  3. Odoo Shell

    Odoo shell 提供了一个简便的操作 Odoo的交互界面, 从 odoo 9.0 开始就是标准功能, 无需安装第三方应用. 本文基于Odoo10 说明 Odoo Shell以及 Odoo Mod ...

  4. Defining custom settings in Odoo

    Unfortunately Odoo documentation doesn’t seem to include any information about adding new configurat ...

  5. Implementation Documentation[转]

    原文地址:http://prasanna-adf.blogspot.tw/2009/04/implementation-documentation.html Following are the lis ...

  6. odoo配置界面设置字段默认值

    转自国外牛人博客:http://ludwiktrammer.github.io/odoo/custom-settings-odoo.html Defining custom settings in O ...

  7. odoo 有哪些文档资源

    // openbook [覆盖 openerp 7 及之前版本] https://doc.odoo.com/     // 最新的 odoo documentation user[覆盖 odoo 9] ...

  8. 第七章 Odoo 12开发之记录集 - 使用模型数据

    在上一篇文章中,我们概览了模型创建以及如何从模型中载入和导出数据.现在我们已有数据模型和相关数据,是时候学习如何编程与其进行交互 了.模型的 ORM(Object-Relational Mapping ...

  9. linux命令总结之date命令

    命令简介: date 根据给定格式显示日期或设置系统日期时间.print or set the system date and time 指令所在路径:/bin/date 命令语法: date [OP ...

随机推荐

  1. leetcode-229-求众数②

    题目描述: 方法一:摩尔投票法 class Solution: def majorityElement(self, nums: List[int]) -> List[int]: candiate ...

  2. [JZOJ6299] 2019.08.12【NOIP提高组A】工厂

    题目 题目大意 工厂内每个人只会操作一些机器. 他们会以随机的顺序来,每次选任意一台机器来操作. 一台机器只能由一个工人来操作. 可以花费一的代价来使某个工人学会一种机器. 问花费最少的代价,使得在所 ...

  3. 【JZOJ6293】迷宫

    description analysis 有没有想起[\(NOIP2018\)]保卫王国? 设\(tr[t][x][y]\)表示线段树上的\(t\)节点代表的区间,从最左边列的\(x\)行到最右边列\ ...

  4. for循环和字典预习

    print("*" *8)for a in range(1,9): print(a,end="")#1-9的奇数print()for a in range(1, ...

  5. 日志框架一logback配置和使用

    把logback或者log4j放在src/main/resources下,Spring容器就可以自动加载日志文件. 前言 Logback是由log4j创始人设计的又一个开源日志组件, 比log4j的性 ...

  6. thinkphp 使用php代码

    Php代码可以和标签在模板文件中混合使用,可以在模板文件里面书写任意的PHP语句代码 ,包括下面两种方式: 直线电机选购 第一种:使用php标签 例如: <php>echo 'Hello, ...

  7. 线段树离散化+区间更新——cf1179C好题

    绝对是很好的题 把问题转化成当第i个询问的答案是数值x时是否可行 要判断值x是否可行,只要再将问题转化成a数组里>=x的值数量是否严格大于b数组里的>=x的值 那么线段树叶子结点维护对于值 ...

  8. Myeclipse从外部导入项目时,jsp和html页面中所有的onclick="return xx()"位置均出现cannot return from outside function() or method()错误

  9. System.Configuration.ConfigurationManager.cs

    ylbtech-System.Configuration.ConfigurationManager.cs 1.程序集 System.Configuration, Version=4.0.0.0, Cu ...

  10. transient在java中的作用

    java 的transient关键字的作用是需要实现Serilizable接口,将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中. trans ...