Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Can i override the delete records method?

Carlos Llamacho
on 10/10/13, 5:56 PM 4,101 views

I want to extend the functionality of the method that deletes the records of module so that when i do, it also deletes some records related to this one. Can this be done?

I thought that overriding the unlink method would work, but i tried and no result.

Here is my code.

def unlink(self, cr, uid, ids, context=None):
        #Call the parent method to eliminate the records.
        super(Specie, self).unlink(cr, uid, ids, context)

        Actions_Model = self.pool.get('ir.actions.act_window')
        Menus_Model = self.pool.get('ir.ui.menu')

        species = self.browse(cr, uid, ids)
        for specie in species:
            menus = specie.menus
            actions = specie.actions
            if menus:
                for menu in menus:
                    Menus_Model.unlink(cr, uid, menu)
            if actions:
                for action in actions:
                    Actions_Model.unlink(cr, uid, action)
        return True

Edit: I tried using the ondelete attribute as suggested but is not working with one2many fields. I have these 2 fields...

'actions': fields.one2many('ir.actions.act_window', 'specie', 'Actions', ondelete='cascade'),
 'menus': fields.one2many('ir.ui.menu', 'specie', 'Menus', ondelete='cascade')

and when i erase my record i want that it erases the field that are in related via these 2. ondelete='cascade' does that? I try and didn't happen.


Daniel Reis

--Daniel Reis--
| 6 7 9
Lisbon, Portugal
--Daniel Reis--

Author of the "Odoo Development Essentials" book.

Applications Manager at Securitas Portugal

Github: https://github.com/dreispt

Twitter; @reis_pt

Daniel Reis
On 10/10/13, 7:05 PM

I think you're complicating: OpenERP already provides the logic you need. Check the ondelete field attribute on the documentation.

Geez, i had completely forgot about that. I have never used it before.

Carlos Llamacho
on 10/11/13, 10:56 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)


Asked: 10/10/13, 5:56 PM
Seen: 4101 times
Last updated: 3/16/15, 8:10 AM