继承多个模型并覆盖其中之一

时间:2019-08-16 13:40:56

标签: python odoo odoo-12

我正在尝试从create覆盖maintenance.request方法来发送我的模板(以将新请求通知维护团队成员)。为此,我还需要继承mail.thread。这是我的问题。

我有这样的model.py

class custom_maintenance_notify(models.Model):
    _name = 'maintenance.request'
    _inherit = ['maintenance.request','mail.thread']

    @api.model
    def create(self, vals):
        record = super(MaintenanceRequest, self).create(vals)

        template_id = self.env.ref('custom_maintenance.new_request_template').id
        if template_id:
            record.message_post_with_template(template_id)
        return record

mail_template.xml像这样:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- <data noupdate="0"> -->
    <data>
        <record id="new_request_template" model="mail.template">
            <field name="name">New maintenance request</field>
            <field name="model_id" ref="maintenance.model_maintenance_request"/>
            <field name="subject">New maintenance request - ${object.company_id.name}</field>
            <field name="partner_to">${",".join(map(str, object.maintenance_team_id.member_ids.mapped('partner_id').ids))}</field>
            <field name="body_html" type="xml">
<div>
Test
</div>
            </field>
            <field name="lang">${object.partner_id.lang}</field>
            <field name="auto_delete" eval="False"/>
            <field name="user_signature" eval="False"/>
        </record>
    </data>
</odoo>

_manifest_这样:

    'depends': ['base','web','maintenance','mail'],

    'data': [
        # ...
        'data/mail_template.xml',
    ],

当我尝试创建新的维护记录时,出现此错误:

  File "/usr/lib/python3/dist-packages/odoo/addons/custom_maintenance/models/models.py", line 35, in create
    record = super(MaintenanceRequest, self).create(vals)
NameError: name 'MaintenanceRequest' is not defined

因此,我尝试将_name更改为MaintenanceRequest,但与此同时,我什至无法升级模型,并且出现此错误:

ValueError: The _name attribute MaintenanceRequest is not valid.

重命名类的结果也相同。

我该如何解决?

1 个答案:

答案 0 :(得分:2)

我已经在另一个问题中写过:

super应该以类名来调用,在您的示例中为custom_maintenance_notify,或者因为您使用的是Python 3,所以可以简单地使用不带参数的super()

    @api.model
    def create(self, vals):
        record = super().create(vals)

        template_id = self.env.ref('custom_maintenance.new_request_template').id
        if template_id:
            record.message_post_with_template(template_id)
        return record

即使您想覆盖mail.thread的(其他语言的抽象模型或接口)方法,也不必再次继承:

class MaintenanceRequest(models.Model):
    _inherit = 'maintenance.request'

    # override a mail.thread method
    @api.model
    def message_new(self, msg_dict, custom_values=None):
        res = super().message_new(msg_dict, custom_values)
        # do stuff
        return res

最后但并非最不重要:尝试遵守Odoo命名约定。这就是我在上面的示例中所做的:类名与Odoo的原始代码完全相同。