Odoo Help

Welcome!

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.

0

How to completely override an inherited method (create,write,unlink...)?

By
Gabriel
on 2/19/15, 4:55 AM 1,539 views

How to override a method without calling the previously inherited one but directly the framework method?

An exemple is that the model product.product has an unlink method which delete the related product template if no more variants exists.

    def unlink(self, cr, uid, ids, context=None):
        unlink_ids = []
        unlink_product_tmpl_ids = []
        for product in self.browse(cr, uid, ids, context=context):
...
            tmpl_id = product.product_tmpl_id.id
            # Check if the product is last product of this template
            other_product_ids = self.search(cr, uid, [('product_tmpl_id', '=', tmpl_id), ('id', '!=', product.id)], context=context)
            if not other_product_ids:
                unlink_product_tmpl_ids.append(tmpl_id)
            unlink_ids.append(product.id)
        res = super(product_product, self).unlink(cr, uid, unlink_ids, context=context)
        # delete templates after calling super, as deleting template could lead to deleting
        # products due to ondelete='cascade'
        self.pool.get('product.template').unlink(cr, uid, unlink_product_tmpl_ids, context=context)
        return res

I would like to override this unlink method to delete the template only if it is a real template (defining attributes) and not a simple single variant template. I have for this created the field is_template. The override method would be modified with:

if not other_product_ids and not product.product_tmpl_id.is_template:

and be placed in a new module.

But this fails as the previously defined method is called (with the super()) and will unlink the template even if it is a "real template". Would you have some suggestion on how to bypass the previously inherited method?

Have you tried just not including the "super" call? In that case, if you have a dependency on the product module, at least only your method will be called.

Ludo - Neobis
on 2/19/15, 5:30 AM
0
Jordan Vrtanoski
On 2/19/15, 7:23 AM

You can call the unlink from the Model (or BaseModel or osv.osv) directly instead of calling super(product_product,self).unlink

Keep in mind that his way you will skipp the whole inheritance chain, so you might need to evaluate which functionality you will lose.

Thank you for your answer. How would you call the unlink method from Model or osv.osv? Does the inheritance chain consist only of all the "def unlink..." placed inside the product_product class or is there something else?

Gabriel
on 2/19/15, 8:11 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

1 follower(s)

Stats

Asked: 2/19/15, 4:55 AM
Seen: 1539 times
Last updated: 3/16/15, 8:10 AM