Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
11199 Widoki

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?

Awatar
Odrzuć

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.

Najlepsza odpowiedź

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.

Awatar
Odrzuć
Autor

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?

Powiązane posty Odpowiedzi Widoki Czynność
2
cze 15
9507
2
gru 22
12488
1
gru 22
3715
1
mar 15
14952
0
mar 15
4186