You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo XML中Domain过滤器失效 求快速修复

Answer

Ah, I see the problem here—this is a super common gotcha with computed fields in Odoo!

Root Cause

Your computed field is_pincode_present uses the default store=False setting, meaning it's not saved to the database. Odoo executes domain filters as database queries, so it can't filter on fields that aren't persisted in the database. That's why your domain is returning all records—it can't evaluate the computed value at the database level.

Here are two quick, reliable fixes you can pick from:


This is the simplest solution if your zip code data doesn't change constantly. We'll tell Odoo to save the computed value to the database, and set up dependencies so it updates automatically when related data changes.

  1. Update the field definition to add store=True, readonly=True, and dependency tracking:

    is_pincode_present = fields.Boolean(
        'Zip Match', 
        compute="_is_pincodes_present", 
        store=True, 
        readonly=True,
        depends=['zip', 'env.user.partner_id.id']
    )
    
  2. Fix the calculation logic to handle empty zip lists and set False explicitly (your original code only set True and left unmatched records with default values):

    def _is_pincodes_present(self):
        # Get the current user's assigned zip codes
        pin_master = self.env['franchise.pincode.master'].search(
            [('name', '=', self.env.user.partner_id.id)], 
            limit=1
        )
        pin_lists = pin_master.mapped("franchise_pincode_line.pincode.pincode") if pin_master else []
        
        for partner in self:
            # Explicitly set True/False for every partner
            partner.is_pincode_present = partner.zip in pin_lists
    

Once you update this, install/update your module, and the domain [('is_pincode_present','=',1)] will work exactly as expected—since the field is now stored in the database, the filter can be executed directly in SQL.


Fix 2: Dynamic Domain Filter (No Stored Field)

If you don't want to store the field (e.g., zip codes change very frequently), you can filter records in Python and pass the filtered IDs to the action's domain.

Add this method to your ResPartner model:

@api.model
def action_view_filtered_partners(self):
    # Get the base action
    action = self.env.ref('your_module_name.action_view_partner_form_one').read()[0]
    
    # Fetch the zip codes for the current user
    pin_master = self.env['franchise.pincode.master'].search(
        [('name', '=', self.env.user.partner_id.id)], 
        limit=1
    )
    pin_lists = pin_master.mapped("franchise_pincode_line.pincode.pincode") if pin_master else []
    
    # Find all partners with matching zip codes
    matching_partner_ids = self.search([('zip', 'in', pin_lists)]).ids
    
    # Update the action's domain to only include matching IDs
    action['domain'] = [('id', 'in', matching_partner_ids)]
    return action

Then, update your XML action to either:

  • Remove the existing domain field, and link the action to this method (by setting name="action_view_filtered_partners"), or
  • Call this method directly from your menu item instead of the original action.

Extra Notes

  • For Fix 1, the depends parameter is critical—it tells Odoo when to re-calculate the field. If your franchise.pincode.master data changes often, you might need to add triggers in that model to force a re-calculation for related partners.
  • Always explicitly set both True and False in computed field logic—leaving values unset can lead to inconsistent behavior.

内容的提问来源于stack exchange,提问作者Baskar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:04:38