def action_confirm(self): if not self.outbound_line: raise UserError('请录入出库明细!') for record in self.outbound_line: if record.quantity < record.available_inventory: database_dict = { "available_inventory": record.available_inventory - record.quantity } outbound = self.env['roke.warehouse.statistics'].search([('code', '=', record.code.id)]) if outbound: outbound.write(database_dict) else: raise UserError('没有该信息!') else: raise UserError('当前数量大于库存数量!') self.warehouse_state = 'confirm'这个在另一个模型中修改不了数据是为什么
时间: 2024-02-10 19:33:17 浏览: 25
这段代码中有一个名为`self.env`的对象,这是Odoo中的一个全局变量,它代表当前模型的操作环境。当你在一个模型中定义一个函数时,该函数只能在该模型的记录上执行,因为该函数的`self.env`变量只能引用该模型的记录。如果你在另一个模型中调用该函数,它将引用该另一个模型的记录。因此,在另一个模型中调用该函数时,你可能无法修改数据,因为`self.env`变量不是在你想要的模型上下文中创建的。
相关问题
def action_confirm(self): if not self.outbound_line: raise UserError('请录入出库明细!') for record in self.outbound_line: if record.quantity < record.available_inventory: database_dict = { "available_inventory": record.available_inventory - record.quantity } outbound = self.env['roke.warehouse.statistics'].search([('code', '=', record.code.id)]) outbound.write(database_dict) else: raise UserError('当前数量大于库存数量!') self.warehouse_state = 'confirm'这个在另一个表中没有数据
如果在 `roke.warehouse.statistics` 表中没有符合搜索条件的记录,则 `search()` 方法将返回空记录集,因此在尝试访问空记录集时会引发异常。您需要在执行 `write()` 操作之前检查是否存在符合条件的记录,如果没有,则需要进行相应的处理,例如创建新的记录或引发警告。
下面是一个可能的解决方案:
```
def action_confirm(self):
if not self.outbound_line:
raise UserError('请录入出库明细!')
for record in self.outbound_line:
if record.quantity < record.available_inventory:
database_dict = {
"available_inventory": record.available_inventory - record.quantity
}
# 检查是否存在符合条件的记录
outbound = self.env['roke.warehouse.statistics'].search([('code', '=', record.code.id)])
if outbound:
outbound.write(database_dict)
else:
# 如果不存在符合条件的记录,可以选择创建新的记录或引发警告
# 例如:
# self.env['roke.warehouse.statistics'].create({
# 'code': record.code.id,
# 'available_inventory': record.available_inventory - record.quantity
# })
raise UserError('找不到符合条件的记录!')
else:
raise UserError('当前数量大于库存数量!')
self.warehouse_state = 'confirm'
```
在上面的示例中,如果没有符合条件的记录,代码将引发一个警告,您也可以选择使用 `create()` 方法创建一个新的记录。请根据您的实际需求进行相应的处理。
解class SigninControl(): def __init__(self, ui, wnd): self.wnd = wnd self.ui = ui ui.signin_confirm.clicked.connect(self.Confirm) ui.signin_return.clicked.connect(self.Return) return
这是一个 Python 类的定义。类的名字是 SigninControl,它接受两个参数:ui 和 wnd。在类的初始化方法 __init__ 中,它将这两个参数分别赋值给类的成员变量 wnd 和 ui。
然后,它将 ui 对象的 signin_confirm 信号与 Confirm 方法进行连接,并将 ui 对象的 signin_return 信号与 Return 方法进行连接。
最后,返回 None。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)