什么是语境?为什么在odoo中使用?
What is context? Why use in odoo?
odoo 中的上下文是什么意思?请举例说明
def change_product_qty(self, cr, uid, ids, context=None):
""" Changes the Product Quantity by making a Physical Inventory. """
if context is None:
context = {}
inventory_obj = self.pool.get('stock.inventory')
inventory_line_obj = self.pool.get('stock.inventory.line')
for data in self.browse(cr, uid, ids, context=context):
if data.new_quantity < 0:
raise UserError(_('Quantity cannot be negative.'))
ctx = context.copy()
ctx['location'] = data.location_id.id
ctx['lot_id'] = data.lot_id.id
if data.product_id.id and data.lot_id.id:
filter = 'none'
elif data.product_id.id:
filter = 'product'
else:
filter = 'none'
inventory_id = inventory_obj.create(cr, uid, {
'name': _('INV: %s') % tools.ustr(data.product_id.name),
'filter': filter,
'product_id': data.product_id.id,
'location_id': data.location_id.id,
'lot_id': data.lot_id.id}, context=context)
line_data = self._prepare_inventory_line(cr, uid, inventory_id, data, context=context)
inventory_line_obj.create(cr, uid, line_data, context=context)
inventory_obj.action_done(cr, uid, [inventory_id], context=context)
return {}
上下文是包含登录用户 ID、语言、时区以及可能添加的任何键值对的字典。它是作为在模型和视图之间传递数据的一种方式。在 Android 中,它类似于在启动意图或 activity 时传递数据包。
odoo 中的上下文是什么意思?请举例说明
def change_product_qty(self, cr, uid, ids, context=None):
""" Changes the Product Quantity by making a Physical Inventory. """
if context is None:
context = {}
inventory_obj = self.pool.get('stock.inventory')
inventory_line_obj = self.pool.get('stock.inventory.line')
for data in self.browse(cr, uid, ids, context=context):
if data.new_quantity < 0:
raise UserError(_('Quantity cannot be negative.'))
ctx = context.copy()
ctx['location'] = data.location_id.id
ctx['lot_id'] = data.lot_id.id
if data.product_id.id and data.lot_id.id:
filter = 'none'
elif data.product_id.id:
filter = 'product'
else:
filter = 'none'
inventory_id = inventory_obj.create(cr, uid, {
'name': _('INV: %s') % tools.ustr(data.product_id.name),
'filter': filter,
'product_id': data.product_id.id,
'location_id': data.location_id.id,
'lot_id': data.lot_id.id}, context=context)
line_data = self._prepare_inventory_line(cr, uid, inventory_id, data, context=context)
inventory_line_obj.create(cr, uid, line_data, context=context)
inventory_obj.action_done(cr, uid, [inventory_id], context=context)
return {}
上下文是包含登录用户 ID、语言、时区以及可能添加的任何键值对的字典。它是作为在模型和视图之间传递数据的一种方式。在 Android 中,它类似于在启动意图或 activity 时传递数据包。