Che*_*uCR 4 python recordset odoo odoo-8
我在模型中添加了一些字段,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)
Run Code Online (Sandbox Code Playgroud)
如果方法需要,我会创建批次_get_lot_id
.如果已经创建了批次,则返回id.这很好用
它非常简单并且可以很好地工作很多次,但有时我会得到以下错误,尽管我restrict_lot_id
只在字典中填充了一个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, *args, **old_kwargs)
File "/[ ... ]/stock/stock.py", line 1993, in create
res = super(stock_move, self).create(cr, uid, vals, context=context)
File "/[ ... ]/openerp/api.py", line 268, in wrapper
return old_api(self, *args, **kwargs)
File "/[ ... ]/openerp/api.py", line 372, in old_api
result = method(recs, *args, **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", line 266, in wrapper
return new_api(self, *args, **kwargs)
File "/[ ... ]/openerp/models.py", line 4126, in create
record = self.browse(self._create(old_vals))
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, *args, **old_kwargs)
File "/[ ... ]/openerp/models.py", line 4323, in _create
recs._validate_fields(vals)
File "/[ ... ]/openerp/api.py", line 266, in wrapper
return new_api(self, *args, **kwargs)
File "/[ ... ]/openerp/models.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)')
Run Code Online (Sandbox Code Playgroud)
我还验证了id到达模块中的原始create
函数内部stock
.这对我没有任何意义.怎么了?
我刚检查过,如果我总是创造它,它运作良好.所以_get_lot_id
我在这里展示的方法有些不对劲
def _get_lot_id(self, n, row, product_id,
picking_type_id, box_quantity, 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', '=', product_id.id),
('name', '=', 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,
'product_id': product_id.id,
})
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
Run Code Online (Sandbox Code Playgroud)
我认为问题出在你的一个代码中,确保在注释代码时重启服务器.但是让我解释为什么这个错误可能发生在odoo中,方法中的self是一个记录集,意味着它可以包含一个或多个记录.当你使用装饰器@ api.multi,depends或者约束时,self可以包含多个记录,以避免这种错误,确保你循环自我.
for rec in self:
# your code here
# always access to rec fields not self here
Run Code Online (Sandbox Code Playgroud)
如果您在执行self
此操作时不循环,则当记录集只有一条记录时可以正常工作,但是当您有更多记录时,这会让您想要获取some_field的值的记录混乱,这里会出现此错误.
但是当你在这里使用装饰器@ api.one时,你告诉odoo在自己传递一条记录所以单身错误永远不会与api.one一起使用不要使用api.one它不建议用于循环.
检查原始插件中的代码,他们在计算字段中使用几乎循环.
当我学习odoo时,这个错误让我发疯.阅读有关装饰器一和多,以了解差异