[英]How to get current user name as related field odoo
<record id="product.product_normal_action_sell" model="ir.actions.act_window"> <field name="name">Product Variants</field> <field name="type">ir.actions.act_window</field> <field name="res_model">product.product</field> <field name="view_mode">kanban,tree,form,activity</field> <field name="context">{"search_default_filter_to_sell":1}</field> <field name="domain">[('user', '=', "Ignored value")]</field> <field name="view_id" ref="product.product_product_tree_view"/> <field name="search_view_id" ref="product.product_search_form_view"/> <field name="help" type="html"> <p class="o_view_nocontent_smiling_face"> Create a new product variant </p><p> You must define a product for everything you sell, whether it's a physical product, a consumable or a service you offer to customers. The product form contains information to simplify the sale process: price, notes in the quotation, accounting data, procurement methods, etc. </p> </field> </record>
我想在登錄時總是更改用戶字段。 我知道我可以使用默認值,但默認功能不會影響現有產品。我想為此用戶相關字段。請建議我。
_inherit = 'product.template'
_description = "Product filter"
category_branch = fields.Many2one('multi.branch', string="Branch Name",related="categ_id.branch_id",rea
class ResUsers(models.Model): """Res Users Model.""" _inherit = 'res.users' @api.model def _get_branch(self): return self.env.user.branch_id branch_id = fields.Many2one('multi.branch', string='Branch', default=_get_branch, help='The branch this user is currently \\ working for.')
donly=真)
相關字段是作為相關(代理)字段的計算字段的一種特殊情況,它提供當前記錄上子字段的值。
相關字段的值是通過遵循一系列相關字段並讀取所達到模型上的字段來給出的。 要遍歷的完整字段序列由related
屬性指定。
self.env.user
不是關系字段的序列,您不能在related
參數中使用它。
您需要將其定義為計算域,以獲取當前用戶的值並在請求時返回。
def _get_current_user(self):
for r in self:
r.user_id = self.env.user
def _search_branch(self, operator, value):
return [('categ_id.branch_id', operator, self.env.user.branch_id.id)]
user = fields.Many2one('res.users', compute='_get_current_user', search='_search_branch')
編輯:
可以通過設置搜索參數來啟用對計算域的搜索。 該值是返回搜索域的方法名稱。
在對模型進行實際搜索之前處理域時會調用搜索方法。 它必須返回一個等同於條件的域: field operator value
。
您需要將搜索域返回的value
參數替換為self.env.user.branch_id.id
然后嘗試在產品模板操作中使用以下域:
[('user', '=', "Ignored value")]
例子:
class ResUsers(models.Model):
_inherit = 'res.users'
branch_id = fields.Many2one('multi.branch')
class ProductCategory(models.Model):
_inherit = 'product.category'
branch_id = fields.Many2one('multi.branch')
class ProductTemplate(models.Model):
_inherit = 'product.template'
user = fields.Many2one("res.users", compute='_get_current_user', search='_search_branch')
def _get_current_user(self):
for r in self:
r.user_branch = self.env.user.id
def _search_branch(self, operator, value):
return [('categ_id.branch_id', operator, self.env.user.branch_id.id)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.