Odoo 10 在 XML 中使用配置值(存储在 ir.values 中)

Odoo 10 use configuration value (stored in ir.values) inside XML

我想在边栏中创建一个显示特定类别产品的菜单。我正在考虑为此任务使用过滤器,这是默认设置的。

但是,我不知道如何在我的 XML 域中使用我的配置值。

我的 XML 代码如下所示:

<record id="my_product_search_form_view" model="ir.ui.view">
    <field name="name">Products Of My Category Search</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_search_view" />
    <field name="arch" type="xml">
        <xpath expr="//search" position="inside">
            <filter string="My Category" name="filter_my_categ" domain="[('categ_id','child_of',my_category)]"/>
        </xpath>
    </field>
</record>

<record id="my_product_action" model="ir.actions.act_window">
    <field name="name">Products Of My Category</field>
    <field name="type">ir.actions.act_window</field>
    <field name="res_model">product.template</field>
    <field name="view_mode">kanban,tree,form</field>
    <field name="context">{"search_default_filter_my_categ":1}</field>
    <field name="search_view_id" ref="my_product_search_form_view" />
</record>

<menuitem id="menu_my_products" name="Products Of my Category"
      parent="menu_product" action="my_product_action"
       />

我希望,当使用模型 'product.template' 将 'my_category' 添加到 ir.values table 时,值将以某种方式添加到上下文中 - 这不是案例 & 我得到一个 Odoo 客户端错误 NameError: name 'my_category' is not defined

有谁知道我如何在 XML 视图中使用 ir.values table 的值 - 或者至少在 [=13] 中调用 python 方法=] 或 domain 标签?或者我的任务还有其他解决方案吗?感谢您的帮助!

我在 odoo v8 中试过了,它对我有用。

首先创建没有域的过滤器,只有上下文。

<filter string="My Category" name="filter_my_categ" domain="[]" context="{'custom_filter':1}"/>

然后我继承了这样的搜索方式

def search(self, cr, uid, args, offset=0, limit=None, order=None,context=None, count=False):                        
        if context.get('custom_filter',False):
            state = self.pool.get('ir.values').get_default(cr, uid, 'sale.order', 'dyn_filter')
            args.append(['state','=',state])
        result= super(sale_order_ext, self).search(cr, uid, args=args, offset=offset, limit=limit, order=order,
            context=context, count=count)

    return result 

就是这样。 谢谢。

除了 Odoo8 中的解决方案(由 Viki Chavada 发布)外,下面是 python 代码对于 Odoo 10 的外观以及 product.template 的扩展:

class ProductTemplate(models.Model):
    _inherit = "product.template"

    @api.model
    def search(self, args, offset=0, limit=None, order=None, count=False):
        if self.env.context.get('custom_filter', False):
            category = self.env['ir.values'].get_default('my.config', 'my_category')
            args.append(['categ_id', 'child_of', category])

        result = super(ProductTemplate, self).search(args=args, offset=offset, limit=limit, order=order, count=count)

        return result