缺少1个必需的位置参数:odoo中的“ self”?

时间:2019-02-10 07:57:15

标签: python odoo self

在我编写此代码的模型中,我正在尝试为此功能向特定用户发送消息

class SkypeBot(models.Model):
    _name = 'my.skype'
    _inherit = ['mail.thread', 'mail.activity', 'res.users']
    _description = 'My Skype'


    skype_login = fields.Char('Your skype Login')
    skype_password = fields.Char('Your skype password')
    skype_message = fields.Char(store=True)

    @api.multi
    def msg(self, message):

        partner_id = self.env['res.users'].search([('id', '=', 2)]).partner_id.id

        _logger.info('^^^^^' * 5)
        _logger.warning(partner_id)
        _logger.info('^^^^^' * 5)

        self.env['mail.message'].create({'message_type': 'notification',
                                         'subtype': self.env.ref('mail.mt_comment').id,  # subject type
                                         'body': message,
                                         'subject': 'Message subject',
                                         'partner_ids': [(4, partner_id), ],
                                         # partner to whom you send notification
                                         })

我想从另一个MySkype类调用此方法(msg):

class MySkype(skpy.SkypeEventLoop):
    def onEvent(self, event):
        if isinstance(event, skpy.SkypeNewMessageEvent):  

            message = ('New message from user {} at {}: \'{} \''.format(event.msg.userId,
                                                                   event.msg.time.strftime('%H:%M dd. %d.%m.%Y'),
                                                                   event.msg.content))


            _logger.info('--------'*5)
            _logger.warning(event)
            _logger.info('--------' * 5)
            _logger.warning(message)
            _logger.info('--------' * 5)

            skype_model.SkypeBot.msg(message=message)

from skpy import Skype

sk = MySkype('+375', '1239qW', autoAck=True)
thread = threading.Thread(target=sk.loop)
thread.start()

但是调用此方法时出现错误

лют 10 10:42:00 PK odoo12[8534]: 2019-02-10 07:42:00,618 8534 INFO ? odoo.addons.bus.models.bus: Bus.loop listen imbus on db postgres
лют 10 10:42:50 PK odoo12[8534]: 2019-02-10 07:42:50,620 8534 INFO odoo12 werkzeug: 127.0.0.1 - - [10/Feb/2019 07:42:50] "POST /longpolling/poll HTTP/1.1" 200 - 35 0.030 53.925
лют 10 10:42:55 PK odoo12[8534]: 2019-02-10 07:42:55,056 8534 INFO ? odoo.addons.skype_bot.skype_send_message: —--------------------------------------
лют 10 10:42:55 PK odoo12[8534]: 2019-02-10 07:42:55,056 8534 WARNING ? odoo.addons.skype_bot.skype_send_message: [SkypeNewMessageEvent]
лют 10 10:42:55 PK odoo12[8534]: Id: 1014
лют 10 10:42:55 PK odoo12[8534]: Type: NewMessage
лют 10 10:42:55 PK odoo12[8534]: Time: 2019-02-10 07:42:54
лют 10 10:42:55 PK odoo12[8534]: MsgId: 1549784574906
лют 10 10:42:55 PK odoo12[8534]: 2019-02-10 07:42:55,057 8534 INFO ? odoo.addons.skype_bot.skype_send_message: —--------------------------------------
лют 10 10:42:55 PK odoo12[8534]: 2019-02-10 07:42:55,057 8534 WARNING ? odoo.addons.skype_bot.skype_send_message: Test!Test!Test!Test!Test!
лют 10 10:42:55 PK odoo12[8534]: 2019-02-10 07:42:55,057 8534 INFO ? odoo.addons.skype_bot.skype_send_message: —--------------------------------------
лют 10 10:43:40 PK odoo12[8534]: Exception in thread Thread-2:
лют 10 10:43:40 PK odoo12[8534]: Traceback (most recent call last):
лют 10 10:43:40 PK odoo12[8534]: File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
лют 10 10:43:40 PK odoo12[8534]: self.run()
лют 10 10:43:40 PK odoo12[8534]: File "/usr/lib/python3.6/threading.py", line 864, in run
лют 10 10:43:40 PK odoo12[8534]: self._target(*self._args, **self._kwargs)
лют 10 10:43:40 PK odoo12[8534]: File "/opt/odoo12/odoo-venv/lib/python3.6/site-packages/skpy/main.py", line 207, in loop
лют 10 10:43:40 PK odoo12[8534]: self.cycle()
лют 10 10:43:40 PK odoo12[8534]: File "/opt/odoo12/odoo-venv/lib/python3.6/site-packages/skpy/main.py", line 196, in cycle
лют 10 10:43:40 PK odoo12[8534]: self.onEvent(event)
лют 10 10:43:40 PK odoo12[8534]: File "/opt/odoo12/odoo-custom-addons/skype_bot/skype_send_message.py", line 33, in onEvent
лют 10 10:43:40 PK odoo12[8534]: skype_model.SkypeBot.msg(message=message)
лют 10 10:43:40 PK odoo12[8534]: TypeError: msg() missing 1 required positional argument: 'self'
лют 10 10:43:40 PK odoo12[8534]: 2019-02-10 07:43:40,702 8534 INFO odoo12 werkzeug: 127.0.0.1 - - [10/Feb/2019 07:43:40] "POST /longpolling/poll HTTP/1.1" 200 - 8 0.003 50.051

我做错了什么以及如何解决这个问题

我尝试使用@ api.model它没有帮助我。 似乎我对odoo环境的初始化有问题,但是我可以确切地了解在哪里

1 个答案:

答案 0 :(得分:3)

正如@ TigerhawkT3所提到的,您将其称为静态方法而不是类方法。

  

在Python中,每个类方法(包括init)的第一个参数   始终是对该类当前实例的引用。通过   按照惯例,此参数始终命名为self¹

为了正确地调用此方法,您需要创建一个类SkypeBot的实例,然后然后调用msg()

sbot = SkypeBot()
sbot.msg(message)

有关将self作为参数的显式声明中使用的语言设计决策的更多信息,请参见this Stack Overflow postGuido van Rossum撰写的本文。