2017-08-17 20 views
0

我有我的课以下字段:添加按钮 'action_open_quants' 承袭 'product.template' 模式 - Odoo V8

class bsi_production_order(models.Model): 
    _name = 'bsi.production.order' 
    _inherit = ['mail.thread','text.paper','product.template'] 

    product_id = fields.Many2one('product.template', string="Product") 
    qty_available = fields.Float(string="Qty Available", related="product_id.qty_available") 

本来就stock模块你得到这个功能:

class product_template(osv.osv): 
    _name = 'product.template' 
    _inherit = 'product.template' 

    def action_open_quants(self, cr, uid, ids, context=None): 
     products = self._get_products(cr, uid, ids, context=context) 
     result = self._get_act_window_dict(cr, uid, 'stock.product_open_quants', context=context) 
     result['domain'] = "[('product_id','in',[" + ','.join(map(str, products)) + "])]" 
     result['context'] = "{'search_default_locationgroup': 1, 'search_default_internal_loc': 1}" 
     return result 

由于我在我的自定义模块上继承了product.template,因此我想在我的视图中显示出相同的功能,所以我只是这样宣布:

<field name="product_id"/> 
<field name="qty_available"/> 
<button class="oe_stat_button" 
    name="action_open_quants" 
    icon="fa-building-o" 
    type="object"> 

本来(上stock模块),现声明如下:

<button class="oe_stat_button" 
    name="action_open_quants" 
    icon="fa-building-o" 
    type="object" attrs="{'invisible':[('type', '=', 'service')]}" groups="stock.group_locations"> 
    <div><field name="qty_available_text"/></div> 
</button> 

现在,它是局部的工作,因为我可以想像与我从Many2one选择产品相关的定量分析师及相关领域,但这与我认为我无意选择的产品无关。

那么,有没有办法像stock模块那样工作?

我希望我已经解释了我自己。

回答

1

您可以修改模块中的_get_products函数以返回要在量化视图中显示的产品。我会做的一种方法是使用上下文中的product_id传递给_get_products功能

在您的看法:

<field name="product_id" context="{'product_tmpl_id': product_id}"/> 

而在你_get_products功能:

def _get_products(self, cr, uid, ids, context=None): 
    products = [] 
    context = context or {} 
    product_tmpl_id = context.get('product_tmpl_id', False) 
    if product_tmpl_id: 
     prodtmpl = self.pool.get('product.template').browse(cr, uid, product_tmpl_id, context=None) 
     if prodtmpl: 
      products += [x.id for x in prodtmpl.product_variant_ids] 
    else: 
     products = #... call super here 
    return products