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 constraint\n\n%s" % tools.ustr(e))ValIDationError: ('ValIDateError',u'Error while valIDating constraint\n\nValueError\nExpected 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(_('ERROR\nThere 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时,这个错误让我发疯.阅读有关装饰器一和多,以了解差异
总结以上是内存溢出为你收集整理的python – 尽管只发送了一个id,为什么我会收到错误“Expected singleton”?全部内容,希望文章能够帮你解决python – 尽管只发送了一个id,为什么我会收到错误“Expected singleton”?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)