1
votes

I am using odoo 10e. What i want to do is i want to set domain criteria inside fields_view_get method

    @api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
    res = super(Customer, self).fields_view_get(
        view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
    doc = etree.XML(res['arch'])
    if view_type == 'tree':
        if self.env.user.partner_id.parent_id.id is False:
            id = self.env.user.id
        else:
            id = self.env.user.partner_id.parent_id.id
        doc.attrib['domain'] = "[('custodians','='," + str(id) + ")]"

    for node_form in doc.xpath("//tree"):
        node_form.attrib['duplicate'] = '0'
        res['arch'] = etree.tostring(doc)
    for node_form in doc.xpath("//form"):
        node_form.attrib['duplicate'] = '0'
        res['arch'] = etree.tostring(doc)
    return res

this is what i did tried. But its not working. You can see why i want to set domain from backend because i have to set user_id based on condition.

Please let me know if i am doing wrong or is there any better way.

Edit

I have defined custodians fields as follow

custodians = fields.Many2one('res.users', string="Custodian", domain=[('groups_id', 'in', [12])],
                             readonly=[('readonly_custodian', '=', True)])

actually when ever a loggedin user create a Customer records we set him as a custodian for that Customer and all i want to do is when that user loggin again he should be able to see his and his parent custodian records

1
You should rarely need to override fields_get. What exactly are you trying to achieve?travisw
I want to add domain filter on tree view, dynamically from back-end. So that i can have condition based domain filterAncient
see everything under this check if view_type == 'tree'. This is the condition i want to achieve in domainAncient

1 Answers

2
votes

It seems like you should be able to achieve this behavior by modifying the domain on the action that loads your tree view.

There are two ways that Window Actions might be defined, but either way, the important part is the domain value.

Old answer:

First way:

<act_window id="..."
            name="..."
            ...
            domain="[('custodians','=',user.partner_id.parent_id and user.partner_id.parent_id.id or user.id)]"/>

Second way:

<record id="..." model="ir.actions.act_window">
    <field name="domain">[('custodians','=',user.partner_id.parent_id and user.partner_id.parent_id.id or user.id)]</field>
</record>

EDIT:

Since it seems user is not accessible in the domain, I think this might work:

Record Rule

<record id="view_ir_rule_restrict_custodians" model="ir.rule">
    <field name="name">Restrict Users to see only their Custodians</field>
    <field name="model_id" ref="model_res_partner"/>
    <!-- Should this be `... or user.partner_id.id`? -->
    <field name="domain_force">[('custodians','=',user.partner_id.parent_id and user.partner_id.parent_id.id or user.id)]</field>
</record>

Note: This is a global rule, so you may want to forcefully remove it for some groups (like managers)

<record id="view_ir_rule_unrestrict_custodians_managers" model="ir.rule">
    <field name="name">Un-Restrict Managers to see any Custodians</field>
    <field name="model_id" ref="model_res_partner"/>
    <field name="domain_force">[(1, '=', 1)]</field>
    <field name="groups" eval="[(4, ref('base.group_sale_manager'))]"/>
</record>

EDIT 2:

As we found in chat, the domain specific to your question is this:

[('custodians','in',user.partner_id.parent_id and [(user.partner_id.parent_id.id),user.partner_id.id] or [user.partner_id.id])]