You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Odoo 11:act_window自定义上下文添加客户视图默认过滤器遇阻

Hey there! Let's tackle this Odoo 11 issue you're facing with adding a company-dependent default search filter for your customer views. I've run into similar scenarios before, so here's what you need to know to fix this:

First, Let's Clear Up the Core Misunderstanding

Your initial approach had a small but critical mistake: search_default_<filter_name> in the action context doesn't take a field value (like a company name or ID) — it takes a 1 or 0 to enable/disable a pre-defined filter. The logic to tie the filter to the user's company belongs in the filter itself, not the action context.

Step 1: Define Your Company-Dependent Filter

First, add a custom filter to your customer search view that targets the current user's company. If you're inheriting the default partner search view, use this XML:

<record id="res_partner_view_search_company_filter" model="ir.ui.view">
    <field name="name">res.partner.view.search.company.filter</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.res_partner_view_search"/>
    <field name="arch" type="xml">
        <!-- Insert the filter after an existing one, adjust the xpath as needed -->
        <xpath expr="//filter[@name='customer']" position="after">
            <filter 
                name="incompany_ids" 
                string="My Company Customers" 
                domain="[('company_id', '=', user.company_id.id)]"
                help="Show only customers from your current company"/>
        </xpath>
    </field>
</record>
  • If your res.partner records can have empty company_id and you want to include those, adjust the domain to [('company_id', 'in', [user.company_id.id, False])].
  • Replace user.company_id.id with user.company_id.name only if your filter needs to match by company name (ID is more reliable and efficient).

Step 2: Update the Action Window to Enable the Filter by Default

Now modify the customer action window to enable this filter automatically when the view loads. Again, using inheritance:

<record id="action_partner_customer_company_default" model="ir.actions.act_window">
    <field name="name">Customers (My Company)</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.action_partner_customer_form"/>
    <field name="context">{'search_default_incompany_ids': 1, 'default_company_id': user.company_id.id}</field>
</record>

The key here is 'search_default_incompany_ids': 1 — this tells Odoo to turn on your custom filter by default. The default_company_id part is optional but helpful to set the default company for new customer records.

Step 3: Fix the View Refresh Issue

If the view doesn't update when the user switches companies, try these fixes:

  • Force context re-evaluation: Add 'no_cache': True to the action context. This ensures Odoo recalculates the context (and thus the filter) every time the view loads, instead of using a cached version.
  • Verify user company setup: Make sure the user has a valid company_id assigned (superusers sometimes don't have one by default, which breaks the filter logic).
  • Manual refresh trigger: If all else fails, you can add a small JavaScript snippet to refresh the view when the company selector changes, but this is a last resort — the above steps should handle most cases.

Why Your Previous Attempts Failed

  • Using "user.company_id.name" directly in the search_default_incompany_ids value was incorrect because that parameter expects a toggle (1/0), not a field value.
  • Even if you tried to pass a filter value directly, Odoo's XML context syntax requires proper reference to user fields, but this approach isn't intended for enabling pre-built filters.

Test It Out

  1. Upgrade your module to load the new view and action.
  2. Log in as a user assigned to a specific company, then open the customer view — you should only see customers linked to that company by default.
  3. Switch companies via the top-right selector, refresh the page, and confirm the filter updates to the new company's records.

内容的提问来源于stack exchange,提问作者A. Cbr

火山引擎 最新活动