zoukankan      html  css  js  c++  java
  • Odoo Documentation : Environment

    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
  • 相关阅读:
    隐藏QQ全部图标,隐藏QQ全部信息
    发放腾讯微博邀请,先到先得、
    关于“5005: 优化字节代码时发生未知错误。”的处理办法
    端口
    xmldocument
    MasterPage
    asp.net ajax
    mysqladmin 设置用户名初始密码报错you need the SUPER privilege for this operation
    实践SSH通道链接国外服务器访问受限网站
    转载 实践与分享:Windows 7怎么获取TrustedInstaller权限【图文教程】
  • 原文地址:https://www.cnblogs.com/dancesir/p/7026018.html
Copyright © 2011-2022 走看看