Access other field of currently logged user In domain in Action window Odoo13

Question:

I have a domain in window action. I want to filter the data by this domain, and I need to acccees another field of currently logged user some like this (uid.branch_ids.ids) and use this field in the domain. I don’t know how to do it Here is my code:

<field name="domain">[('type', '=', 'out_invoice'),('invoice_line_ids.branch_id', 'in', uid.branch_ids.ids)]</field>

when I run this code gives me this error:

object has no attribute ‘branch_ids’

But I have the branch_ids in my res.users model

Asked By: SH EB

||

Answers:

The window action domain is evaluated with the following context:

allowed_company_ids: Array [ 1 ]
context_today: function context_today()​
current_date: Object { __is_type: false, _value: "2022-08-22" }
datetime: Object { __is_type: false, timedelta: {…}, datetime: {…}, … }
default_type: "out_invoice"
lang: "en_US"
relativedelta: Object {__init__: __init__(), __name__: "relativedelta", __is_type: true, … }
time: Object { __is_type: false, strftime: {…} }
tz: "Europe/London"
tz_offset: function tz_offset()
uid: 2

As you can see, the user record is not available.

Edit:

You can’t use the field traversal to reach a user id from the branch record. Unfortunately you can’t have a user record in the evaluation context because an object of type res.users can’t be JSON serializable.

The window action domain is used to filter data in the search function, so you can override the search function and customize your domain there.

Example:

class AccountInvoice(models.Model):
    _inherit = 'account.move'

    @api.model
    def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None):
        domain = domain or []
        if self.env.context.get('default_type') == 'out_invoice':
            domain.append(['invoice_line_ids.branch_id', 'in', self.env.user.branch_ids.ids])
        return super(AccountInvoice, self).search_read(domain, fields, offset, limit, order)
Answered By: Kenly
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.