2

So I have this piece of code:

class QuerySetUpdateOverriden(QuerySet, object):
    def update(self, *args, **kwargs):
        super().update(*args, **kwargs)
        if hasattr(self, method):
            self.method()
            return

I want to override the update method and if the second class that it inherits has a specific method, call that too.

But I get an error on the line containing the if statement saying "name method is not defined".

Why is this happening and how can I fix it?

Thanks.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Octavian Niculescu
  • 1,177
  • 1
  • 3
  • 24
  • 1
    What/where is `method` defined? If you're trying to get the name `update` programmatically, [this may help](https://stackoverflow.com/questions/63066627/how-to-get-a-method-name-in-python). – sj95126 Sep 18 '22 at 14:55
  • The parameter for `hasattr` should be a string, because you don't pass variables to functions, you pass **values**. – Karl Knechtel Sep 18 '22 at 15:33

1 Answers1

1

you should pass a string with the name, so:

if hasattr(self, 'method'):
    self.method()
    return
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555