【问题标题】:Populate DropDown List dynamically动态填充下拉列表
【发布时间】:2014-05-09 09:42:21
【问题描述】:

我正在尝试从 OpenERP 中另一个 DDL 的值填充我的 DDL(选择)。这是我尝试过的代码。

这是我的视图 XML:

<h1>
    <label for="categ1" string="Parent category"/>
         <field name="categ1" on_change="Product_Category_OnChange(categ1)" />
</h1>
<newline/>
<h1> 
    <label for="my_products" string="Products" /> 
         <field name="my_products" />
</h1>

我的_columns 对于这个观点是这样的:

_columns = {
     'categ1':fields.many2one('product.category','Parent Category',required=True),
     'my_products':fields.many2one('product.product','Products')
}

我的onchange 函数是这样的:

def Product_Category_OnChange(self,cr,uid,ids,categ1):
    pro_id={}
    cr.execute('select ... where parent_id='+str(categ1))
    res = cr.fetchall()
    for i in range(len(res)):
            pro_id[i]=res[i]
    return {'domain':{'my_products': pro_id}}

问题是,我没有得到my_products 的过滤值,而是得到my_products 中的所有值。请让我知道,我做错了什么,或者指出我正确的方向。谢谢

【问题讨论】:

  • 您可以在 xml 或 py 中使用域过滤器,为什么您尝试在 on_change 上这样做?
  • 我在 Py 文件和 xml 中使用域过滤器进行了尝试,但实际上由于模型中的一些自联接和父子关系,我必须使用查询来执行此操作。这就是我在on_change 方法中这样做的原因。任何建议有什么问题。
  • 如果你能详细说明或发布一些链接,以便我有一些想法
  • 检查@dhana的答案

标签: python openerp onchange openerp-7


【解决方案1】:

我认为您只想显示属于该类别的产品。

def Product_Category_OnChange(self,cr,uid,ids,categ1, context=None):
    pro_id={}
    product_obj = self.pool.get('product.category')
    if not categ1:return {}
    categ_obj = product_obj.browse(cr, uid, categ1)

    return {'domain':{'my_products':[('categ_id','=',categ_obj.id)]}}

或在xml中

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

【讨论】:

  • 我们可以创建一个下拉字段,因为我的值正在返回,但它们都与 ID 一起显示在 1 个文本框中。
  • 你修改过产品的name_get函数吗
  • 我使用 cr.execute 在我的 onchange 函数中运行 sql 查询以返回值。这些值还可以,但希望以下拉菜单或选择类型菜单的形式实现它们
  • 在xml中你可以使用widget="selection"。但我不知道为什么它在下拉列表中显示 id
  • 我的代码中没有下拉列表。当我使用 many2one 时,它​​给了我 TypeError: Object 29 has no method 'split'
【解决方案2】:

像这样。它对我有用

{'domain':{'my_products':[('id','=',categ1)]}}

语法是这样的,

def Product_Category_OnChange(self,cr,uid,ids,categ1, context=None):
        pro_id={}
        product_obj = self.pool.get('product.category')
        print product_obj.browse(cr, uid, categ1)
        cr.execute('select * from product_category where parent_id='+str(categ1))
        res = cr.fetchall()
        for i in range(len(res)):
            pro_id[i]=res[i]
        return {'domain':{'my_products':[('id','=',categ1)]}}

更新:

我明白了,首先,您必须制作一个自定义模块。

将以下代码放入您的 .py 文件中。

class product(osv.osv):
    _inherit = 'product.product'

    def name_get(self, cr, uid, ids, context=None):
        res = []
        cr.execute('select ... where parent_id='+str(ids[0]))
        resource = cr.fetchall()
        for r in resource:
            res.append((r.id, r.name)) # My assumption
        return res

看这个http://bazaar.launchpad.net/~openerp/openobject-addons/trunk/view/head:/product/product.py#L778

希望对你有所帮助。

【讨论】:

  • 实际上我想从我发布的查询中填充 my_products 。我有一个要求,我应该直接从查询中获取 my_product 的数据,而不是通过使用 categ1 id 进行过滤。我希望现在很清楚。谢谢
猜你喜欢
  • 2023-04-03
  • 2020-06-26
  • 2015-12-11
  • 2019-06-06
  • 2014-03-01
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多