AnJ
AnJ

Reputation: 614

Inherit multiple model and override method of one of them

I'm trying to override create method from maintenance.request to send my template (to notify maintenance team members about new request). To do this I also need to inherit mail.thread. And here is my problem.

I have model.py like this:

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 like this:

<?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>

And _manifest_ like this:

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

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

When I'm trying to create new maintenance record I get this error:

  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

So I tried to change _name to MaintenanceRequest but with this I can not even upgrade the model and I get this error:

ValueError: The _name attribute MaintenanceRequest is not valid.

Same result with renaming class as well.

How do I fix this?

Upvotes: 0

Views: 857

Answers (1)

CZoellner
CZoellner

Reputation: 14768

As i've already written in the other question:

super should be called with the class name, which is custom_maintenance_notify in your example, or because you're using Python 3 you can simply use super() without parameters.

    @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

And even if you want to override mail.thread's (it's an abstract model or interface in other languages) methods, you don't have to inherit again:

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

Last but not least: try to stick to the Odoo Naming Convention. That's what i've done in the example above: the class name is exactly the same as in Odoo's original code.

Upvotes: 2

Related Questions