2017-07-03 71 views
2

我想在显示特定类别的产品的侧边栏中创建一个菜单。我正在考虑为此任务使用过滤器,默认情况下这是设置的。Odoo 10在XML中使用配置值(存储在ir.values中)

但是,我不知道如何在我的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" 
     /> 

我希望,这将“my_category”与模式“product.template”的ir.values表中的值时,会以某种方式被加入到上下文 - 这是不是这样&我得到一个Odoo客户端错误NameError:名字“my_category”没有定义

有谁知道我可以用我的XML视图中的ir.values表的价值 - 或至少在context或内调用python方法标签?还是有我的任务的另一个解决方案?谢谢你的帮助!

回答

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 

就这样。 谢谢。

+0

就像一个魅力!谢谢! – IstaLibera

0

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