This question has been flagged
4 Replies
8560 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

Avatar
Discard
Best Answer

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

Avatar
Discard
Best Answer

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"/>
Avatar
Discard
Best Answer

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.

Avatar
Discard
Best Answer

Hi,

Try this code:

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

Avatar
Discard
Author

thanks for replying but it didnot work .

What issue you facing ?

Author

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

I have update code pls check it. and try it

Author

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

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

Author

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

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

Author

didnot get you. Can you explain plz

You need chnage sequnse of your customer module menu.

Author

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?

did not get you

Author

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

set domain for that.

Author

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