odoo 从另一个模型发布消息

odoo posting messages from another model

我的代码中有函数 returns 一些数据到另一个模型:

this is the def in the main model:
    @api.multi
    def lost_cheque(self):
        return {
            'type': 'ir.actions.act_window',
            'name': 'Lost Cheque',
            'view_mode': 'form',
            'view_type': 'form',
            'res_model': 'lost.cheque',
            'target': 'new',
            'context': 'None'
        }

这是子模型中的定义:

@api.multi
    def lost_cheque(self):
        cheque_obj = self.env['cheque.master'].browse(self.env.context.get('active_id'))
        if cheque_obj.state == 'used':
            cheque_obj.write({'state': 'lost'})
        if cheque_obj.state in ('issued', 'pending', 'printed'):
            cheque_config = self.env['cheque.config.settings'].search([], order='id desc', limit=1)
            if not cheque_config.cheque_journal_p_id:
                raise UserError(_('Set Cheque Payment Journal under Settings !!!'))
            journal_id = cheque_config.cheque_journal_p_id.id
            line_ids = [
                (0, 0,
                 {'journal_id': journal_id, 'account_id': cheque_obj.bank_name.pdc_account_id.id,
                  'name': cheque_obj.name,
                  'amount_currency': 0.0, 'debit': cheque_obj.amount}),
                (0, 0, {'journal_id': journal_id, 'account_id': cheque_obj.partner_account_id.id, 'name': '/',
                        'amount_currency': 0.0, 'credit': cheque_obj.amount, 'partner_id': cheque_obj.partner_id.id})
            ]
            vals = {
                'journal_id': journal_id,
                'ref': cheque_obj.name,
                'date': self.date_lost,
                'line_ids': line_ids,
            }
            account_move = self.env['account.move'].create(vals)
            account_move.post()
            cheque_obj.write({'state': 'lost','lost_date': self.date_lost, 'account_move_ids': [(4, account_move.id)]})

如果 def 完成,我想做的是 post 聊天中的一条消息,例如:

    msg = _("cheque  is lost")
    self.message_post(body=msg)

但如果我将此代码放在第一个 def 中 即使被调用的向导不完整,消息仍然会被 posted 你有什么建议?

通过删除操作字典条目尝试使用已经存在的context

    @api.multi
    def lost_cheque(self):
        return {
            'type': 'ir.actions.act_window',
            'name': 'Lost Cheque',
            'view_mode': 'form',
            'view_type': 'form',
            'res_model': 'lost.cheque',
            'target': 'new',
        }

然后将消息发布添加到您的 wizard/assistant 方法的末尾(我更改了一些名称并对其进行了一些格式化):

    @api.multi
    def lost_cheque(self):
        cheque = self.env['cheque.master'].browse(self.env.context.get('active_id'))
        if cheque.state == 'used':
            cheque.state = 'lost'
        elif cheque.state in ('issued', 'pending', 'printed'):
            cheque_config = self.env['cheque.config.settings'].search(
                [], order='id desc', limit=1)
            if not cheque_config.cheque_journal_p_id:
                raise UserError(_('Set Cheque Payment Journal under Settings !!!'))
            journal_id = cheque_config.cheque_journal_p_id.id
            line_ids = [
                (0, 0,
                 {'journal_id': journal_id,
                  'account_id': cheque.bank_name.pdc_account_id.id,
                  'name': cheque.name,
                  'amount_currency': 0.0, 'debit': cheque.amount}),
                (0, 0, {'journal_id': journal_id,
                        'account_id': cheque.partner_account_id.id,
                        'name': '/',
                        'amount_currency': 0.0,
                        'credit': cheque.amount,
                        'partner_id': cheque.partner_id.id})
            ]
            vals = {
                'journal_id': journal_id,
                'ref': cheque.name,
                'date': self.date_lost,
                'line_ids': line_ids,
            }
            account_move = self.env['account.move'].create(vals)
            account_move.post()
            cheque.write({'state': 'lost',
                          'lost_date': self.date_lost,
                          'account_move_ids': [(4, account_move.id)]})
        msg = _("cheque is lost")
        cheque.message_post(body=msg)