【问题标题】:Odoo 10 use configuration value (stored in ir.values) inside XMLOdoo 10 在 XML 中使用配置值(存储在 ir.values 中)
【发布时间】:2017-12-06 10:39:54
【问题描述】:

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

但是,我不知道如何在我的 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 表中时,该值会以某种方式添加到上下文中 - 事实并非如此,我得到一个 Odoo 客户端错误 NameError:名称“my_category”未定义

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

【问题讨论】:

    标签: python xml openerp odoo-10


    【解决方案1】:

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

    就是这样。 谢谢。

    【讨论】:

    • 像魅力一样工作!谢谢!
    【解决方案2】:

    除了 Odoo8 中的解决方案(由 Viki Chavada 发布)之外,以下是用于 Odoo 10 和扩展 product.template 的 Python 代码的样子:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多