Odoo9中product_print_zpl_barcode模块生成标签触发TypeError求助
TypeError: generate() takes exactly 1 argument (5 given) in Odoo 9 Custom Module
这个错误的根源很明确:你的generate方法定义不符合Odoo 9的模型方法调用规范。
当你在视图里用type="object"的按钮调用模型方法时,Odoo会自动向方法传递4个额外参数:数据库游标(cr)、用户ID(uid)、当前记录ID列表(ids)、上下文(context),再加上方法自身的self,总共5个参数。但你现在的方法只定义了self,就会触发参数不匹配的TypeError。
解决方案(两种可选,推荐第二种)
方案1:使用Odoo旧API风格
修改你的generate方法定义,补上必要的参数,然后手动获取当前记录:
def generate(self, cr, uid, ids, context=None): # 获取当前 wizard 记录 record = self.browse(cr, uid, ids[0], context=context) # 把原有代码里的self替换成record,逻辑保持不变 assert record.barcode if len(record.barcode) != 13: raise UserError(_( "This wizard only supports EAN13 for the moment. Barcode '%s' " "has %d digits instead of 13") % ( record.barcode, len(record.barcode))) if not record.copies: raise UserError(_("The number of copies cannot be 0")) if record.barcode_type in ('price', 'weight'): vals = record._prepare_price_weight_barcode_type() elif record.barcode_type == 'product': vals = record._prepare_product_barcode_type() else: raise UserError(_( "Barcode Type %s is not supported for the moment") % record.barcode_type) vals.update({ 'state': 'step2', 'zpl_filename': 'barcode_%s.zpl' % vals['barcode'], }) record.write(vals) action = self.pool.get('ir.actions.act_window').for_xml_id( cr, uid, 'product_print_zpl_barcode', 'product_print_zpl_barcode_action', context=context) action.update({ 'res_id': record.id, 'context': context, 'views': False }) return action
方案2:使用Odoo新API(推荐,更简洁)
Odoo 9已经支持新API,只需要给方法加上@api.multi装饰器,Odoo会自动处理参数传递:
from openerp import api # ... 其他原有导入代码 @api.multi def generate(self): # 确保当前只有一条wizard记录(wizard通常都是单条记录) self.ensure_one() # 原有代码逻辑完全保留即可 assert self.barcode if len(self.barcode) != 13: raise UserError(_( "This wizard only supports EAN13 for the moment. Barcode '%s' " "has %d digits instead of 13") % ( self.barcode, len(self.barcode))) if not self.copies: raise UserError(_("The number of copies cannot be 0")) if self.barcode_type in ('price', 'weight'): vals = self._prepare_price_weight_barcode_type() elif self.barcode_type == 'product': vals = self._prepare_product_barcode_type() else: raise UserError(_( "Barcode Type %s is not supported for the moment") % self.barcode_type) vals.update({ 'state': 'step2', 'zpl_filename': 'barcode_%s.zpl' % vals['barcode'], }) self.write(vals) action = self.env['ir.actions.act_window'].for_xml_id( 'product_print_zpl_barcode', 'product_print_zpl_barcode_action') action.update({ 'res_id': self.id, 'context': self._context, 'views': False }) return action
验证步骤
- 保存修改后的代码
- 重启Odoo服务
- 在Odoo后台更新你的
product_print_zpl_barcode模块 - 再次点击"Generate Label"按钮,错误应该会消失
内容的提问来源于stack exchange,提问作者Dhouha
相关产品推荐
相关产品推荐

