首页 > 解决方案 > 如何获取当前用户名作为相关字段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.')

多利=真)

标签: pythonodoo-13

解决方案


相关字段是计算字段的一种特殊情况,它是相关(代理)字段,它提供当前记录上子字段的值。

通过遵循一系列关系字段并读取到达模型上的字段来给出相关字段的值。要遍历的字段的完整序列由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)]

推荐阅读