سؤال

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:

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

My values are filtered but are all displayed on the field separated by commas. I want them to be listed as dropdown . I used many2one but the values didont seem to be filtered. Therefore I am having problems achieving my values as dropdown list view. Please help me. Regards

هل كانت مفيدة؟

المحلول

my_products should be a many2one field related to product.product.

'my_products':fields.many2one('product.product',string="Product")

if you want a filtered product list by using on_change on category you have to return a new domain filter for my_products like (pls use the orm methods instead of direct queries):

def Product_Category_OnChange(self,cr,uid,ids,categ1):
    product_obj = self.pool.get('product.product')
    product_ids = product_obj.search(cr, uid, [('cat_id','=',categ1)]) #maybe cat_id is wrong field name
    return {'domain':{'my_products':[('id','in',product_ids)]}}

:-)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top