首页 > 解决方案 > return self.browse((self._ids[key],)) IndexError: tuple index out of range : 当从 Odoo V13 中的讨论发送消息时

问题描述

每当在讨论模块上发送消息时,都会发生以下错误,我们将其配置为向用户 firebase_id 发送通知。

File "/odoo/odoo-server/addons/mail/models/mail_channel.py", line 368, in message_post
    message = super(Channel, self.with_context(mail_create_nosubscribe=True)).message_post(message_type=message_type, moderation_status=moderation_status, **kwargs)**
File "/odoo/custom/addons/elite_event_management_api/controllers/message.py", line 34, in message_post
    registration_id = channel_partner_id.partner_id.user_ids[0].firebase_id
  File "/odoo/odoo-server/odoo/models.py", line 5624, in _getitem_
    return self.browse((self._ids[key],))

IndexError: tuple index out of range

这是代码 - 所有用户都无法在讨论模块上发送消息。

import logging
from odoo import models, api
from odoo.exceptions import AccessDenied, UserError
logger = logging.getLogger(__name_)
class MailThred(models.AbstractModel):
    _inherit = "mail.thread"

    @api.returns('mail.message', lambda value: value.id)
    def message_post(self, *,
                     body='', subject=None, message_type='notification',
                     email_from=None, author_id=None, parent_id=False,
                     subtype_id=False, subtype=None, partner_ids=None, channel_ids=None,
                     attachments=None, attachment_ids=None,
                     add_sign=True, record_name=False,
                     **kwargs):

        res = super(MailThred, self).message_post(body=body, subject=subject, message_type=message_type,
                     email_from=email_from, author_id=author_id, parent_id=parent_id,
                     subtype_id=subtype_id, subtype=subtype, partner_ids=partner_ids, channel_ids=channel_ids,
                     attachments=attachments, attachment_ids=attachment_ids,
                     add_sign=add_sign, record_name=record_name,
                     **kwargs)
        message_subtype_id = self.env['mail.message.subtype'].sudo().search([('name', 'ilike', 'Discussions')])
        if res.message_type == 'comment' and res.subtype_id.id == message_subtype_id.id:
            for each in res.channel_ids:
                for channel_partner_id in each.channel_last_seen_partner_ids:
                    if channel_partner_id.partner_id.id != res.author_id.id:
                        from . import FCMManager as fcm
                        registration_id = channel_partner_id.partner_id.user_ids[0].firebase_id
                        if registration_id:
                            try:
                                tokens = [str(registration_id)]
                                message_title = "ControlE@ERP - CHAT"
                                message_body = res.body
                                fcm.sendPush(message_title, message_body, tokens)
                                _logger.info('ControlE@ERP Alert- NEW CHAT MESSAGE SENT')
                            except Exception as e:
                                _logger.info('not sent')
        return res

标签: pythonodooodoo-10odoo-13

解决方案


您在 channel_partner_id.partner_id.user_ids 中没有用户,因此您正在尝试从空列表中获取第 0 个元素,因此请检查您的代码并重试。


推荐阅读