2017-05-25 68 views
1

之間是什麼不同:創建或sudo創建odoo

test = self.env['my.example'].sudo().create({'id':1, 'name': 'test'}) 

test = self.env['my.example'].create({'id':1, 'name': 'test'}) 

所有例如工作,但什麼是優勢,當放須藤?

回答

6

調用sudo()(不帶參數)在調用create()之前將使用admin(超級用戶)用戶標識集來更新記錄集。這意味着您的記錄集上的其他方法調用將使用管理員用戶,並因此繞過訪問權限/記錄規則檢查[source]。 sudo()也採用可選參數user,該參數是將在環境中使用的用戶的標識(res.users)(默認爲SUPERUSER_ID)。

當不使用 sudo(),如果誰調用你的方法,用戶不會對 my.example模式 create權限,然後調用 create將失敗,並 AccessError

由於訪問權限/記錄規則不適用於超級用戶,因此應謹慎使用sudo()。此外,它可能會產生一些不良影響,例如。在多公司環境中混合來自不同公司的記錄,由於高速緩存失效而導致的額外重新獲取(請參閱環境交換,在Model Reference中)。

+0

好的答案,在我的例子中,我不使用Sudo ..... – Pointer

1

您可以在odoo -> models.py -> def sudo()的Odoo代碼中查看sudo的意見。

0

返回附加到提供的 用戶的此記錄集的新版本。

By default this returns a ``SUPERUSER`` recordset, where access 
    control and record rules are bypassed. 

    It is same as: 

    from odoo import api, SUPERUSER_ID 

    env = api.Environment(cr, SUPERUSER_ID, {}) 

    In this example we pass SUPERUSER_ID in place of uid at the time of creating a Enviroment. 

    If you are not use Sudo() then the current user need permission to 
    create a given object. 


    .. note:: 

     Using ``sudo`` could cause data access to cross the 
     boundaries of record rules, possibly mixing records that 
     are meant to be isolated (e.g. records from different 
     companies in multi-company environments). 

     It may lead to un-intuitive results in methods which select one 
     record among many - for example getting the default company, or 
     selecting a Bill of Materials. 

    .. note:: 

     Because the record rules and access control will have to be 
     re-evaluated, the new recordset will not benefit from the current 
     environment's data cache, so later data access may incur extra 
     delays while re-fetching from the database. 
     The returned recordset has the same prefetch object as ``self``.