Search code examples
pythonodoo-13

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>

I want to always change user field when login. I know i can use the default but default function will not effect the exiting product.I want to user related field for this.Pls suggest me.

_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=True)


Solution

  • Related fields are a special case of computed fields that are related (proxy) fields, which provide the value of a sub-field on the current record.

    The value of a related field is given by following a sequence of relational fields and reading a field on the reached model. The complete sequence of fields to traverse is specified by the related attribute.

    self.env.user is not a sequence of relational fields, you can't use it in related parameter.

    You need to define it as a computed field to get the value of the current user and return it when requested.

    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')
    

    Edit:
    searching on a computed field can be enabled by setting the search parameter. The value is a method name returning a Search domains.

    The search method is invoked when processing domains before doing an actual search on the model. It must return a domain equivalent to the condition: field operator value.

    You need to replace the value parameter returned by the search domain to self.env.user.branch_id.id then try to use the following domain in the product template action:

    [('user', '=', "Ignored value")]
    

    Example:

    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)]