添加按钮 'action_open_quants' 到继承的 'product.template' 模型 - Odoo v8

Add button 'action_open_quants' to inherited 'product.template' model - Odoo v8

我的 class 上有这些字段:

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 模块上一样工作?

希望我已经解释清楚了。

您可以将模块中的 _get_products 函数修改为 return 您想要在量化视图中显示的产品。我要做的一种方法是使用上下文将 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