1
votes

I have search view where I am trying to filter quants grouping by product ('group_by': 'product_id') and filter by location ('search_my_quants': 1) but it seems quants are grouping only by product and not filtered by location. I have no idea why.

search view:

<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="Quants">
            <field name="location_id"/>
            <field name="product_id"/>
            <field name="lot_id" />
            <group expand='0' string='Filters'>
                <filter name="work_hardware" string="Hardware" domain="[('product_id.attribute', '=', 'work_hardware')]"/>
                <filter name="hide_reserved" string="Hide Reserved" domain="[('reservation_id', '=', False)]"/>
            </group>
            <group expand='0' string='Group by...'>
                <filter string='Product' name="productgroup" context="{'group_by': 'product_id'}"/>
           </group>
        </search>
    </field>
</record>

xml part

<record model="ir.actions.act_window" id="action_stock_quant_my">
    <field name="name">Mine</field>
    <field name="res_model">stock.quant</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="view_id" ref="view_stock_quant_tree"/>
    <field name="search_view_id" ref="view_stock_quant_search"/>
    <field name="context">{'group_by': 'product_id', 'search_my_quants': 1, 'search_default_hide_reserved': 1, 'search_default_work_hardware':1}</field>
</record>

python part

class stock_quant(models.Model):
    _inherit = "stock.quant"

    @api.model
    def search(self, args, offset=0, limit=None, order=None, count=False):
        context = self.env.context or {}
        if context.get('search_my_quants'):
            args.append(('location_id.partner_id.id', '=', self.env.user.partner_id.id))
        return super(stock_quant, self).search(args, offset, limit, order, count=count)

If I am not using group by filter in search view then I have filtering location. How can I use both filters together?

SOLVED:

On search view I updated filter productgroup with domain which tend to find current user's location.

<filter string="Employee hardware" name="mano" context="{'group_by': 'product_id'}" domain="[('location_id.partner_id.user_ids.id', '=', uid)]"/>
2
Why don't you implement the search as real filter and call it as default?CZoellner
I updated my question with more information and added search view. There are all default filters as real filters except search_my_quants.fueggit
That wasn't my question :-P I meant: Why don't you implement the "My Quants" search as a filter in search view and call it in your menu action like the other default searches?CZoellner
And i think there is one pair of brackets too much in your args.appendCZoellner
Because I just don't know how to do that..fueggit

2 Answers

0
votes

You can write as following :

<record model="ir.actions.act_window" id="action_stock_quant_my">
    <field name="name">Mine</field>
    <field name="res_model">stock.quant</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="view_id" ref="view_stock_quant_tree"/>
    <field name="search_view_id" ref="view_stock_quant_search"/>
    <field name="context">{'search_default_productgroup': 1, 'search_my_quants': 1, 'search_default_hide_reserved': 1, 'search_default_work_hardware':1}</field>
</record>
0
votes

I had this problem before I noticed that when I use group by the filter that I used in python (overriding search method) won't work.

When you use group by you should override read_group because it's the one called when you group record:

    @api.model
    def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
        context = self.env.context or {}
        domain = domain or []
        if context.get('search_my_quants'):
            domain.append(('location_id.partner_id.id', '=', self.env.user.partner_id.id))
        return super(stock_quant, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)

Remember this when you override search method to extend the domain to override the read_group method to handle the same case. At least with this solution you don't have to always group by product when you select the filter.