Odoo Help

Welcome!

This community 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.

0

How to create a filter for many2one filed on form?

By
arsalan
on 5/8/14, 4:10 AM 2,239 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'),
         'my_products':fields.many2one('product.product','Products',size=64),

            }


deg_form()

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">

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

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

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


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

please help me fix this

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

Hello

You can put filter in dynamic fashion

example:

.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:

    pro_tmp_obj=self.pool.get('product.template')

    prod_obj=self.pool.get('product.product')

    prod_categ_ids=pro_tmp_obj.search(cr,uid,['categ_id','=',categ_id])
    pro_ids=prod_obj.search(cr,uid,['product_tmpl_id','in',[prod_categ_ids])



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

my_class()

################
XML file format:

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

<field name="product_id"/>

0

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3918
| 5 3 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

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

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

<button name="show_product" string="SHOW PRODUCT" type="object"/>
0
senthil
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.

0

Jagdish Panchal

--Jagdish Panchal--
2674
| 5 3 6
Gandhinagar, India
--Jagdish Panchal--

OpenERP Developer

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

Hi,

Try this code:

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

thanks for replying but it didnot work .

arsalan
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

arsalan
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: )>"

arsalan
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. ?

arsalan
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

arsalan
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?

arsalan
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

arsalan
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

arsalan
on 5/8/14, 11:25 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

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