Odoo XML中Domain过滤器失效:按邮编筛选合作伙伴失败
Why This Is Happening
Your is_pincode_present field is a computed field with store=False (the default for computed fields in Odoo). Domain filters in Odoo run directly against the database, and since this field isn't persisted to the database, the filter [('is_pincode_present','=',1)] can't actually match anything—Odoo essentially ignores the condition, which is why you're seeing all partners instead of filtered results.
Plus, you can't just set store=True here because your logic depends on the current logged-in user (their associated franchise pincodes). Storing the field would fix the domain issue, but the value would be locked to the user who triggered the computation, not the user viewing the list later.
Quick Fix 1: Embed the Logic Directly in the Domain (Simplest)
Skip the computed field entirely and write your pincode check directly in the action's domain. This avoids any issues with computed fields and keeps the logic tied to the current user.
Update your XML action's domain line to this:
<field name="domain">[('zip', 'in', env['franchise.pincode.master'].search([('name','=',user.partner_id.id)], limit=1).mapped('franchise_pincode_line.pincode.pincode'))]</field>
This directly replicates the logic from your Python compute method, but runs it at the database query level for the domain filter.
Quick Fix 2: Add a Search Method for the Computed Field (Reusable)
If you need to reuse this filter elsewhere (like in view filters or other actions), add a custom search method to your computed field. This tells Odoo how to translate the is_pincode_present condition into a database-compatible domain.
Update your Python code:
class ResPartner(models.Model): _inherit = "res.partner" is_pincode_present = fields.Boolean( 'zip', compute="_is_pincodes_present", search="_search_is_pincode_present" # Add this line ) def _is_pincodes_present(self): pin_lists = self.env['franchise.pincode.master'].search( [('name','=',self.env.user.partner_id.id)], limit=1 ).mapped("franchise_pincode_line.pincode.pincode") for partner in self: partner.is_pincode_present = partner.zip in pin_lists # Add this new search method def _search_is_pincode_present(self, operator, value): # Replicate the pincode list logic pin_lists = self.env['franchise.pincode.master'].search( [('name','=',self.env.user.partner_id.id)], limit=1 ).mapped("franchise_pincode_line.pincode.pincode") # Return the actual database filter return [('zip', 'in', pin_lists)]
Now your original XML domain [('is_pincode_present','=',1)] will work as expected—Odoo will use the _search_is_pincode_present method to generate a valid database query.
Key Notes
- Always avoid storing computed fields that depend on the current user—this will lead to inconsistent results across different users.
- Test with a user who has an associated
franchise.pincode.masterrecord (with pincodes) to make sure the filter returns the right partners.
内容的提问来源于stack exchange,提问作者Baskar

