将按钮'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工作?

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

您可以修改模块中的_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

相关内容

最新更新