def unlink(self): audit = self.env['approval.flow'].search([('approval_cannot_run_id','=','删除')]) print('111111111111111111',audit) management = self.env['roke.dispatch.order.management'].search([('code', '=', audit.condition.code)]).limit(1) if management: raise UserError("不能删除") return super(Rokedispatchordermanagement, self).unlink() 这个方法报错这么修改
时间: 2024-01-19 11:03:26 浏览: 69
可以尝试将代码修改为以下形式:
```
def unlink(self):
audit = self.env['approval.flow'].search([('approval_cannot_run_id', '=', '删除')])
print('111111111111111111', audit)
if audit:
management = self.env['roke.dispatch.order.management'].search([('code', '=', audit.condition.code)]).limit(1)
if management:
raise UserError("不能删除")
return super(Rokedispatchordermanagement, self).unlink()
```
主要修改的地方是在判断 `management` 是否存在之前,先判断 `audit` 是否存在。如果 `audit` 不存在,那么也就不需要再去查询 `management`,避免了可能出现的查询错误。
相关问题
def unlink(self): model = self.env['approval.flow'].search( [('model_id', '=', 'roke.dispatch.order.management.price.approve')]) audit = self.env['approval.flow'].search([('approval_cannot_run_id', '=', '删除')]) management = self.env['roke.dispatch.order.management.price.approve'].search([('order_id.id', '=', self.id)]) for record in management: if model: if audit and record.approval_status == '通过': raise UserError("应付费用审核通过不能删除") return super(Rokedispatchordermanagement, self).unlink()这个方法只可以一个一个删除怎么修改可以删除多条
可以使用以下代码实现批量删除功能:
```
def unlink(self):
model = self.env['approval.flow'].search([('model_id', '=', 'roke.dispatch.order.management.price.approve')])
audit = self.env['approval.flow'].search([('approval_cannot_run_id', '=', '删除')])
management = self.env['roke.dispatch.order.management.price.approve'].search([('order_id.id', 'in', self.ids)])
for record in management:
if model and audit and record.approval_status == '通过':
raise UserError("应付费用审核通过不能删除")
return super(Rokedispatchordermanagement, self).unlink()
```
这里使用了`self.ids`来获取当前选中的多条记录的id列表,然后在`management`中使用`'order_id.id', 'in', self.ids`来查询这些记录。这样就可以同时处理多条记录了。
class ApprovalFlow(models.Model): _name = 'approval.flow' _description = u'审批流程' approval_cannot_run_id = fields.Many2many('execute.function.not.list', 'function_not_rel', u'完成后不能执行操作', help=u'审批流程完成后不能能执行的操作,修改,删除') class Rokedispatchordermanagement(models.Model): _name = 'roke.dispatch.order.management' _inherit = ['mail.thread', 'mail.activity.mixin'] _description = "派船单" _rec_name = "code" client = fields.Many2one('roke.partner', string='客户', tracking=True, required=True, ) department_manager = fields.Many2one('res.users', string='创建人', tracking=True, default=lambda self: self.env.user.id) box_number = fields.Char(string='箱号') shipping_companies = fields.Char(string='船公司') field_station = fields.Char(string='场站') ship_name_voyage = fields.Char(string='船名航次') port_of_discharge = fields.Char(string='卸货港') box_type = fields.Many2one('roke.box.type', string='箱型') code = fields.Char(string='单据编号', track_visibility='onchange') def unlink(self): audit = self.env['approval.flow'] for record in audit: if record.approval_can_run_id == '删除': raise UserError("不能删除") return super(Rokedispatchordermanagement, self).unlink() approval.flow模型在另一个文件中在roke.dispatch.order.management重新删除方法不能删除approval_can_run_id值等于删除的
根据你的代码,我理解您想要在删除Rokedispatchordermanagement模型的记录之前,检查approval_can_run_id值是否等于“删除”,如果是,则不允许删除。但是,您的代码中有几个错误。
首先,您需要将“unlink”方法的重写代码放在“Rokedispatchordermanagement”模型中,而不是在“approval.flow”模型中。
其次,您应该在每个记录上检查approval_can_run_id的值,而不是在“approval.flow”模型上进行操作。您可以使用“self”关键字来引用当前记录。
最后,您需要使用“in”运算符来检查approval_can_run_id是否包含“删除”值,而不是使用“==”运算符来比较。
下面是更正后的代码:
```
class Rokedispatchordermanagement(models.Model):
_name = 'roke.dispatch.order.management'
_inherit = ['mail.thread', 'mail.activity.mixin']
_description = "派船单"
_rec_name = "code"
client = fields.Many2one('roke.partner', string='客户', tracking=True, required=True)
department_manager = fields.Many2one('res.users', string='创建人', tracking=True, default=lambda self: self.env.user.id)
box_number = fields.Char(string='箱号')
shipping_companies = fields.Char(string='船公司')
field_station = fields.Char(string='场站')
ship_name_voyage = fields.Char(string='船名航次')
port_of_discharge = fields.Char(string='卸货港')
box_type = fields.Many2one('roke.box.type', string='箱型')
code = fields.Char(string='单据编号', track_visibility='onchange')
def unlink(self):
for record in self:
if '删除' in record.approval_can_run_id.mapped('name'):
raise UserError("不能删除")
return super(Rokedispatchordermanagement, self).unlink()
```
这个更正后的代码应该可以达到您想要的效果。
阅读全文