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. python中与time模块和datetime模块相关操作

    使用time模块计时程序运行时长 import time time_start = time.time() #程序主体部分 time_end = time.time() print('总耗时: ', ...

  2. python中的 += 语法的使用

    python中有个缩略的写法,如下 a = a +1 等同于 a +=1 发现了一个有趣之处,+=的写法中间不能有空格,否则报错,测试如下 Python 3.7.1 (v3.7.1:260ec2c36 ...

  3. Socket.EndReceive 方法 (IAsyncResult)

        .NET Framework (current version)   其他版本 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 . ...

  4. SCRIPT7002: XMLHttpRequest: 网络错误 0x2ef3的解决方法

    最近在使用jquery easyui datagrid 对页面布局,发现有时在IE下会接收不到数据并报错: SCRIPT7002: XMLHttpRequest: 网络错误 0x2ef3, 由于出现错 ...

  5. day18_文件处理_迭代器_生成器

    #!/usr/bin/env python # -*- coding:utf-8 -*- # ********************day18_文件处理_迭代器_生成器 ************** ...

  6. day 68 Django基础四之模板系统

      Django基础四之模板系统   本节目录 一 语法 二 变量 三 过滤器 四 标签Tags 五 模板继承 六 组件 七 自定义标签和过滤器 八 静态文件相关 一 语法   模板渲染的官方文档 关 ...

  7. day 53 Django基础二之URL路由系统

    Django基础二之URL路由系统   本节目录 一 URL配置 二 正则表达式详解 三 分组命名匹配 四 命名URL(别名)和URL反向解析 五 命名空间模式 一 URL配置 Django 1.11 ...

  8. 通过aapt查看apk包名和第一个启动的activity

    步骤: ps:aapt是sdk 自带的一个工具,在sdk\builds-tools目录下: 1. cmd启动控制台, 默认是c盘,输入“d:” 即可转到D盘目录 2. 到D盘后 输入cd 子文件目录转 ...

  9. FTP、FTPS、SFTP概览

    1. 基本概念 FTP:File Transfer Protocol FTPS:FTP over SSL.构建在SSL/TLS(Secure Socket Layer/Transport Layer ...

  10. 4_3.springboot2.x之默认访问首页和国际化

    1.默认访问首页 1.引入thymeleaf和引入bootstrap <!--引入thymeleaf--> <dependency> <groupId>org.sp ...