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 use drop down field in OpenERP?

By
arsalan
on 5/12/14, 2:50 AM 3,527 views

Hello to all , I have been trying to use a drop down menu list in my openerp module. I have a many2one field containing categories . This field is associated with an onchange function. This onchange function is returning new values to 2nd field. Now everything is working ok but I want to use a dropdown type menu in the 2nd field so that I can select the values from it. My Python code is as follows:

class deg_form(osv.osv):

    _name = "deg.form"
    _columns = {
         'categ1':fields.many2one('product.category','Parent Category',required=True),
         'my_products':fields.char('Product',size=64)

            }

    def Product_Category_OnChange(self,cr,uid,ids,categ1):
            pro_id=[]
            cr.execute('select id,name from product_template where  categ_id in (select id from product_category where parent_id in (select id from product_category where parent_id='+str(categ1)+'))  union select id,name from product_template where  categ_id in (select id from product_category where parent_id='+str(categ1)+') union select id,name from product_template where categ_id='+str(categ1))


    res = cr.fetchall()
    for pid,name in res:
            pro_id.append((pid,name))
        return {'value':{'my_products':pro_id}}

here is my xml code:

 <field name="categ1" on_change="Product_Category_OnChange(categ1)" />

Please help me to achieve this. Thanks n Regards

Hello Arsalan, If my ans is correct, then please approve it.

Hardikgiri Goswami
on 5/12/14, 11:35 AM
5

Hardikgiri Goswami

--Hardikgiri Goswami--
1123
| 2 1 5
Rajkot, India
--Hardikgiri Goswami--

OpenERP/Odoo Customization/Integration Expert / Python Programmer

Skype ID : hardikgiri.goswami

Hardikgiri Goswami
On 5/12/14, 4:32 AM

You don't need to write on change method just remove that method and try below code.

in Python file:

'categ1':fields.many2one('product.category','Parent Category',required=True),
'my_products':fields.many2one('product.product','Product')

and in XML file:

<field name="categ1" on_change="Product_Category_OnChange(categ1)" />
<field name="my_products" domain="[('categ_id','=',categ1)]"/>
0

Jagdish Panchal

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

OpenERP Developer

Jagdish Panchal
On 5/12/14, 3:14 AM

Hi,

Try this code '

my_products':fields.many2one('product.product','Product')

def Product_Category_OnChange(self,cr,uid,ids,categ1):
        pro_id=[]
        cr.execute('select id from product_template where  categ_id in (select id from product_category where parent_id in (select id from product_category where parent_id='+str(categ1)+'))  union select id,name from product_template where  categ_id in (select id from product_category where parent_id='+str(categ1)+') union select id,name from product_template where categ_id='+str(categ1))


    res = cr.fetchall()
    for pid in res:
            pro_id.append(pid)
    return {'value':{'my_products':pro_id}}

On doing this , I m having error : TypeError: Object [object Array] has no method 'split'

arsalan
on 5/12/14, 3:27 AM
for pid,name in res:
        pro_id.append(pid)  use this code in your onchange function.
Jagdish Panchal
on 5/12/14, 3:31 AM

Now its giving me this -> TypeError: Object 29 has no method 'split'

arsalan
on 5/12/14, 3:35 AM

have you upgrade your module ??if not then upgrade module and try this

Jagdish Panchal
on 5/12/14, 3:40 AM

yes I upgraded , but no change

arsalan
on 5/12/14, 3:47 AM

I have update ans pls check it.

Jagdish Panchal
on 5/12/14, 4:17 AM

yes I tried but getting TypeError: Object 29 has no method 'split'

arsalan
on 5/12/14, 4:22 AM

Hi return {'value':{'my_products':pro_id[0]}} try this.

Jagdish Panchal
on 5/12/14, 4:31 AM

the error is resolved and I can view the drop down but they are not filtered . It is showing all values of product.product

arsalan
on 5/12/14, 4:37 AM

my_products':fields.many2one('product.product','Product', domain="[('categ_id','=',categ1)]") add this in py file upgrade module it will work.

Jagdish Panchal
on 5/12/14, 4:41 AM

no , still the values are not filtered . I think product.product will not be there , Since because of product.product its showing all products

arsalan
on 5/12/14, 4:53 AM

Add this in py file 'my_products':fields.many2one('product.product','Product') and add this in xml file <field name="my_products" domain="[('categ_id','=',categ1)]"/>

Jagdish Panchal
on 5/12/14, 4:58 AM

its ok for some categories , like u see I have used union in query just to make sure that I get values filtered in all type of categories therefore this approach is not accepted.

arsalan
on 5/12/14, 5:04 AM

Your problem is solve ? or not ?

Jagdish Panchal
on 5/12/14, 5:20 AM

no my problem is not solved , if I use many2one the values are not filtered and if I use selection field then it will have predefined values

arsalan
on 5/12/14, 6:38 AM
Jagdish Panchal
on 5/12/14, 6:48 AM

that was another scenario and it worked for some categories but my query is now working for all categories and bringing filtered products . Only problem here is how to show those values as selection or dropdown list

arsalan
on 5/12/14, 7:27 AM

can selection field be used for dynamic values. Like I want to return values to selection field. Is that possible?

arsalan
on 5/12/14, 8:52 AM

I did not get.

Jagdish Panchal
on 5/12/14, 8:55 AM

I have like 10 values returning from onchange function . They all are shown on 1 normal field which is weird . I want to use many2one or selection to have a dropdown menu . Thats it .

arsalan
on 5/12/14, 9:01 AM

You can use many2one field but give widget='selection' in xml file

Jagdish Panchal
on 5/12/14, 10:35 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/12/14, 2:50 AM
Seen: 3527 times
Last updated: 3/16/15, 8:10 AM