Odoo搜索视图无法同时应用按产品分组与按位置筛选
search_my_quants Not Working with group_by in Odoo Stock Quant Search View Hey there, let's break down why your search_my_quants filter isn't kicking in when you're grouping by product_id in the stock.quant search view. I’ve tackled similar Odoo quirks before, so here’s what to check and fix:
1. First, Verify Your Search View’s Filter Definition
Your code snippet cuts off, so let’s start with the basics: make sure your search view’s arch section actually includes the search_my_quants filter properly. It should look something like this:
<record id="view_stock_quant_search" model="ir.ui.view"> <field name="name">stock.quant.search</field> <field name="model">stock.quant</field> <field eval="1" name="priority"/> <field name="arch" type="xml"> <search string="Search Quants"> <!-- Your existing search fields here --> <group expand="0" string="Group By"> <filter string="Product" name="group_product" context="{'group_by': 'product_id'}"/> </group> <!-- Ensure this filter exists with correct context --> <filter name="my_quants" string="My Locations" domain="[]" context="{'search_my_quants': 1}"/> </search> </field> </record>
If the my_quants filter is missing or has a typo in the context key, that’s an easy fix. Also, double-check you’ve actually selected this filter in the search interface—it’s easy to forget to toggle it on!
2. Fix Context Filtering for Grouped Queries
Odoo’s group-by functionality sometimes skips context-driven domain filters because grouped queries use a different execution path. The default search_my_quants logic might only apply to regular searches, not grouped ones.
To enforce the filter regardless of grouping, override the _search method of the stock.quant model:
from odoo import models, api class StockQuant(models.Model): _inherit = 'stock.quant' @api.model def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None): # Apply "My Locations" filter if the context flag is active if self.env.context.get('search_my_quants'): # Adjust this line to match your system's user-location mapping # Example: Get locations directly assigned to the current user user_locations = self.env.user.stock_location_ids.ids if user_locations: args.append(('location_id', 'in', user_locations)) # Pass modified args to the original search method return super()._search(args, offset=offset, limit=limit, order=order, count=count, access_rights_uid=access_rights_uid)
This ensures the location filter is applied every time search_my_quants is enabled, even when grouping results. Tweak the user_locations line to fit your setup (some systems use warehouse assignments instead of direct location links).
3. Check for View Inheritance Conflicts
Another module might be overriding your search view and altering the search_my_quants context or group-by behavior. To check:
- Go to Settings > Technical > User Interface > Views
- Search for
view_stock_quant_search - Look at the Inherited Views tab to see if other modules are modifying this view. If you find one, verify it isn’t overriding your filter or group-by settings.
4. Quick Sanity Check
Disable the product_id grouping and test if search_my_quants works. If it does, that confirms the issue is tied to grouping interacting with context filters, and the _search override above should resolve it.
内容的提问来源于stack exchange,提问作者fueggit

