Reputation: 580
I want to create a menu in the sidebar which shows products of a specific category. I was thinking to use a filter for this task, which is set by default.
However, I don't know how to use the value of my configuration inside my XML domain.
Here is how my XML code looks like:
<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"
/>
I hoped, that when adding 'my_category' to the ir.values table with the model 'product.template' the value will somehow be added to the context - which is not the case & I get an Odoo Client Error NameError: name 'my_category' is not defined
Does anyone know how I can use values of the ir.values table inside my XML view - or at least call a python method inside the context
or domain
tags? Or is there another solution for my task? Thanks for your help!
Upvotes: 2
Views: 1113
Reputation: 580
In Addition to the solution in Odoo8 (posted by Viki Chavada), here is how the python code looks like for Odoo 10 & extending the 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
Upvotes: 0
Reputation: 1524
I tried this in odoo v8 and it works for me.
First create your filter without domain only with context.
<filter string="My Category" name="filter_my_categ" domain="[]" context="{'custom_filter':1}"/>
Then i inherited the search method like this.
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
That's all. Thank you.
Upvotes: 1