Odoo XML中Domain过滤器失效 求快速修复
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:
Fix 1: Make the Computed Field Stored (Recommended)
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.
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'] )Fix the calculation logic to handle empty zip lists and set
Falseexplicitly (your original code only setTrueand 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
domainfield, and link the action to this method (by settingname="action_view_filtered_partners"), or - Call this method directly from your menu item instead of the original action.
Extra Notes
- For Fix 1, the
dependsparameter is critical—it tells Odoo when to re-calculate the field. If yourfranchise.pincode.masterdata changes often, you might need to add triggers in that model to force a re-calculation for related partners. - Always explicitly set both
TrueandFalsein computed field logic—leaving values unset can lead to inconsistent behavior.
内容的提问来源于stack exchange,提问作者Baskar

