python – 尽管只发送了一个id,为什么我会收到错误“Expected si
我在模型stock.move中添加了一些字段,然后导入CSV文件以创建一些记录.我查看了所有行,然后逐个创建记录,如下所示:
lot_id = self._get_lot_id( n,row,product_id,picking_type_id,box_quantity,product_uom_qty ) move = { 'auto_lot_name': False,'box_quantity': 2,'client_order_ref': '581002','date': datetime.datetime(2017,6,24,19,55,52,372648),'invoice_state': '2binvoiced','location_dest_id': 9,'location_id': 12,'name': 'Product name','partner_id': 487,'partner_shipping_id': 488,'picking_type_id': 2,'price_unit': 4.0,'pricelist_id': 1,'product_code': u'36033','product_id': 3,'product_uom': 3,'product_uom_qty': 6.0,'restrict_lot_id': 12222,# lot_id 'tax_id': [(4,67)] } result = self.env['stock.move'].create(move) 如果需要,我在方法_get_lot_id中创建批次.如果已经创建了批次,则返回id.这很好用 它非常简单并且可以很好地工作很多次,但有时候我会得到以下错误,尽管我在字段中只填充了一个id而只填充了一个id字段restrict_lot_id.它看起来像是附加了前一个循环的批次ID.怎么可能?我的数据库坏了吗? File "/[ ... ]/import_moves/models/stock_picking_import_wizard.py",line 129,in _generate_moves_from_csv result = self.env['stock.move'].create(move) File "/[ ... ]/openerp/api.py",line 266,in wrapper return new_api(self,*args,**kwargs) File "/[ ... ]/openerp/api.py",line 508,in new_api result = method(self._model,cr,uid,**old_kwargs) File "/[ ... ]/stock/stock.py",line 1993,in create res = super(stock_move,self).create(cr,vals,context=context) File "/[ ... ]/openerp/api.py",line 268,in wrapper return old_api(self,line 372,in old_api result = method(recs,**kwargs) File "/[ ... ]/connector/producer.py",line 48,in create ##> strange because this module is not installed record_id = create_original(self,vals) File "/[ ... ]/openerp/api.py",**kwargs) File "/[ ... ]/openerp/models.py",line 4126,in create record = self.browse(self._create(old_vals)) File "/[ ... ]/openerp/api.py",**old_kwargs) File "/[ ... ]/openerp/models.py",line 4323,in _create recs._validate_fields(vals) File "/[ ... ]/openerp/api.py",line 1285,in _validate_fields raise ValidationError("Error while validating constraintnn%s" % tools.ustr(e)) ValidationError: ('ValidateError',u'Error while validating constraintnnValueErrornExpected singleton: stock.production.lot(12286,12287)') 我还验证了id到达了stock模块中原始create函数的内部. 我刚检查过,如果我总是创造它,它运作良好.所以我在这里展示的方法_get_lot_id有些不对劲 def _get_lot_id(self,n,product_uom_qty): lot_id_name = row.get('lot_id_name',False) if lot_id_name != '': if picking_type_id == STOCK_PICKING_TYPE_OUT: lot_ids = self.env['stock.production.lot'].search([ ('product_id','=',product_id.id),('name',lot_id_name) ]) if len(lot_ids) == 0: try: lot_id = self.env['stock.production.lot'].create({ 'name': lot_id_name,'product_id': product_id.id,}) except Exception: raise Warning(_('The lot could not be created. ' 'nROW: %s') % n) return lot_id.ensure_one().id if len(lot_ids) == 1: return lot_ids[0].id else: raise Warning(_('ERRORnThere is more than one lot with the same name for that product.' 'nROW: %s') % n) elif picking_type_id == STOCK_PICKING_TYPE_IN: lot_ids = self.env['stock.production.lot'].search([ ('product_id',lot_id_name) ]) if len(lot_ids) == 1: return lot_ids[0].id try: lot_id = self.env['stock.production.lot'].create({ 'name': lot_id_name,}) return lot_id.id except Exception: raise Warning(_('The lot could not be created. ' 'nROW: %s') % n) else: if picking_type_id == STOCK_PICKING_TYPE_OUT: raise Warning(_('The lot is required for outgoing moves. ' 'nROW: %s') % n) elif picking_type_id == STOCK_PICKING_TYPE_IN: # set "auto_lot_name = True" >> this is set by default,so the lot is automatically created return False 解决方法
我认为问题出在你的一个代码中,确保在注释代码时重启服务器.
但是让我解释为什么这个错误可能发生在odoo中,方法中的self是一个记录集,意味着它可以包含一个或多个记录.当你使用装饰 @ api.multi,depends或约束这里self可以包含多个记录,以避免这种错误,确保你循环自我. for rec in self: # your code here 如果你在self.some_field时没有循环,那么当记录集只有一条记录时可以正常工作但是当你有更多记录时,这会让你想要获得some_field的值的记录混乱,这里你会得到这个错误. 但是当你在这里使用装饰器@ api.one时,你告诉odoo在自己传递一条记录,所以单独的错误永远不会与api.one一起使用不要使用api.one它不建议用于循环. 检查原始插件中的代码,他们在计算字段中使用几乎循环. 当我学习odoo时,这个错误让我发疯.阅读有关装饰器一和多,以了解差异 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |