【问题标题】:Odoo search view can't apply both filters: group by product and filter by locationOdoo 搜索视图不能同时应用两个过滤器:按产品分组和按位置过滤
【发布时间】:2018-05-03 07:17:25
【问题描述】:

我有搜索视图,我试图按产品('group_by':'product_id')过滤量化人员分组并按位置过滤('search_my_quants':1),但似乎量化人员仅按产品分组,而不是按地点。我不知道为什么。

搜索视图:

<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部分

<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部分

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)

如果我没有在搜索视图中使用group by filter,那么我有过滤位置。如何同时使用这两个过滤器?

已解决:

在搜索视图中,我更新了过滤器 productgroup,其中包含倾向于查找当前用户位置的域。

<filter string="Employee hardware" name="mano" context="{'group_by': 'product_id'}" domain="[('location_id.partner_id.user_ids.id', '=', uid)]"/>

【问题讨论】:

  • 为什么不将搜索实现为真正的过滤器并将其称为默认值?
  • 我用更多信息更新了我的问题并添加了搜索视图。除了 search_my_quants 之外,所有默认过滤器都是真正的过滤器。
  • 这不是我的问题 :-P 我的意思是:为什么不将“我的量化”搜索作为搜索视图中的过滤器,并像其他默认搜索一样在菜单操作中调用它?
  • 我认为你的args.append中有一对括号太多了
  • 因为我只是不知道该怎么做..

标签: xml python-2.7 odoo odoo-10 odoo-9


【解决方案1】:

你可以这样写:

<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>

【讨论】:

  • 是的,但问题是在树形视图中,我将所有现有的量化按产品分组,而不是从特定位置 (search_my_quants) 按产品分组..
  • @fueggit 你能在上下文中使用’search_default_my_quants’: 1吗?默认情况下,它应该在加载视图时应用该搜索过滤器。
  • @travisw 我试图将我的操作上下文更新为 {'search_default_productgroup': 1, 'search_default_my_quants': 1, 'search_default_hide_reserved': 1, 'search_default_work_hardware':1 } 但随后我将所有位置的所有产品归为一组,尽管我只需要一个指定的位置..
【解决方案2】:

在我注意到当我使用在 python 中使用的过滤器分组时(覆盖搜索方法)不起作用之前,我遇到了这个问题。

当您使用 group by 时,您应该覆盖 read_group,因为它是您分组记录时调用的:

    @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)

当您覆盖 search 方法以扩展域以覆盖 read_group 方法以处理相同的情况时,请记住这一点。 至少使用此解决方案,您不必在选择过滤器时始终按产品分组。

【讨论】:

    猜你喜欢
    • 2019-07-27
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    相关资源
    最近更新 更多