Odoo Help


How to create a filter for many2one filed on form?

on 5/8/14, 4:10 AM 3,564 views

Hello to all , I have 3 fields which in my module page.2 are for product categories and 1 for product. The categories fields consist of different categories. Now I want to add filter so that what ever category I set in the field, only those product should list in the products field . It is a type of sorting or filtering. I dont have much idea of using filter in such a scenerio under form view. Here is my code.py:

class deg_form(osv.osv):
    _name = "deg.form"
    _columns = {
         'categ1':fields.many2one('product.category','Parent Category'),
         'categ2':fields.many2one('product.category','Child Category'),



and here is its xml:

<record id="mywin_form_view" model="ir.ui.view">
            <field name="name">mywin.form</field>
            <field name="model">deg.form</field>          
            <field eval="7" name="priority"/>
            <field name="arch" type="xml">
                <form string="FORM DATA" version="7.0">

                       <label for="categ1" string="Parent category"/>
                           <field name="categ1" />

                       <label for="categ2" string="Child category"/>
                           <field name="categ2" />

                       <label for="my_products" string="Products" domain = "[('categ1','=',True)]"/> 
                           <field name="my_products"/> 

                 <button name="show_product" string="SHOW PRODUCT" type="action"/>

please help me fix this

shashank verma
On 5/8/14, 7:03 AM


You can put filter in dynamic fashion


.py file format:

class my_class(): columns={'categ_id1':fields.many2one('product.category',Category),'product_id':fields.many2one('product.product',Products)}

def my_onchange_func(self,cr,uid,ids,categ_id,context):

if categ_id:




    return {'domain':{'product_id':[('id','in',pro_ids)]}}


XML file format:

<field name="categ_id1" on_change="my_onchange_func"/>

<field name="product_id"/>


Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
| 5 5 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 5/8/14, 6:46 AM

Try this: This shows the products that have category set from categ1 and categ2 field

      <label for="my_products" string="Products" /> 
      <field name="my_products" domain = "[('categ_id','in',[categ1, categ2])]"/> 

For a button to call a function in PY FILE, we use type="object"

<button name="show_product" string="SHOW PRODUCT" type="object"/>
On 5/8/14, 4:54 AM

Try this

'my_products':fields.many2one('product.product','Products',size=64, domain="[('categ_id', '=', categ1)]")

Coz the many2one field for product_category in product_template is categ_id and product_product is based on product_template.


Jagdish Panchal

--Jagdish Panchal--

| 5 4 6
Gandhinagar, India
--Jagdish Panchal--

OpenERP Developer

Jagdish Panchal
On 5/8/14, 4:19 AM


Try this code:

'my_products':fields.many2one('product.product','Products',size=64, domain="[('categ1', '=', categ1)]")

thanks for replying but it didnot work .

on 5/8/14, 4:25 AM

What issue you facing ?

Jagdish Panchal
on 5/8/14, 4:40 AM

raise ValueError("Invalid field %r in leaf %r" % (left, str(leaf))) ValueError: Invalid field 'categ1' in leaf "<osv.ExtendedLeaf: ('categ1', '=', 'categ1') on product_product (ctx: )>" Ok

on 5/8/14, 4:42 AM

I have update code pls check it. and try it

Jagdish Panchal
on 5/8/14, 4:45 AM

now I am having this error : ValueError: Invalid field 'categ1' in leaf "<osv.ExtendedLeaf: ('categ1', '=', 12) on product_product (ctx: )>"

on 5/8/14, 4:51 AM

HI try this 'my_products':fields.many2one('product.product','Products',size=64, domain="[('categ_id', '=', categ1)]")

Jagdish Panchal
on 5/8/14, 5:00 AM

thanks a lot ,this worked. do you have any idea to set a route in openerp so that when we login , the first page we see is our module. ?

on 5/8/14, 5:01 AM

Change the sequence of your module main menu same as "Messaging"

Jagdish Panchal
on 5/8/14, 5:04 AM

didnot get you. Can you explain plz

on 5/8/14, 6:14 AM

You need chnage sequnse of your customer module menu.

Jagdish Panchal
on 5/8/14, 7:19 AM

yes thanks again . as you saw I have two categ fields . 1 is parent and other is child . But I want to do like when I set a categ in 1st field, then the 2nd field should just show the child of that parent , not all . Do I have to write function for this , to search for the child of that parent?

on 5/8/14, 7:23 AM

did not get you

Jagdish Panchal
on 5/8/14, 7:39 AM

means like when I set a category in first field . and then when I try to search any category in the other field , then it should show only the child categories of that parent category which I set in the 1st field

on 5/8/14, 7:44 AM

set domain for that.

Jagdish Panchal
on 5/8/14, 8:09 AM

but I can't filter through parent categories. look if I give values in all 3 categories then its ok . but if I try to give only parent_id , then it doesnot show anything although it should show everything in that parent_id

on 5/8/14, 11:25 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 5/8/14, 4:10 AM
Seen: 3564 times
Last updated: 3/16/15, 8:10 AM