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

Hello there,

I have overrided the name_search method on the product.product model.

Beginning of my Python code :

class product_product(models.Model):
    _inherit = "product.product"

    @api.model
    def name_search(self, name='', args=None, operator='ilike', limit=100):

        _logger.error("    args :: %s", str(args))

        res = super(product_product, self).name_search(name, args, operator, context=self._context, limit=limit)
   
        return res


I want to modify the name_search method only if it is triggered from a purchase.order.line. If the name_search method is trigerred from a sale.order.line, I want to use the original name_search method.

How to differentiate the place where the method is triggered?

I noted that when the name_search method is triggered from a purchase.order.line, args are :

args :: [['purchase_ok', '=', True]]

I noted that when the name_search method is triggered from a sale.order.line, args are :

args :: [['sale_ok', '=', True]]


Should I use these different argument to differentiate the two places where the name_search method is triggerred?


May be I could make something like

if 'sale_ok' in args, use original name_search method,

If 'purchase_ok' in args, use the new overriden name_search method


What do you think about it?






Awatar
Odrzuć
Najlepsza odpowiedź

I think you are right , in you search function check if args :: [['sale_ok', '=', True]] just call the super and if args :: [['sale_ok', '=', True]] write your customized search

Awatar
Odrzuć
Autor

I thought It was a cheap way to manage this situation... Thanks for your comment.

Najlepsza odpowiedź

If you wanna make sure that where something is getting called, the best way to do it is by using a context value, that way you could be completely sure that you are not messing up anything else, like adding a context attr to the field in question in the view or on the specific action and check for that specific value in the context in your method override to just add the modifications only if the expected value is in the context

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
3
mar 17
9481
0
maj 16
3370
2
gru 15
3482
0
sie 17
3731
3
lis 16
6601