django-cacheback icon indicating copy to clipboard operation
django-cacheback copied to clipboard

Support for @method_decorator in Python 3.5 and later

Open benwhalley opened this issue 5 years ago • 2 comments

At present using this inside a class based view:

@method_decorator(cacheback(lifetime=60))
def get_context_data(...):
    ...

Causes an error: AttributeError: 'functools.partial' object has no attribute '__module__'

The error arises in jobs.prepare_args which uses ("%s:%s" % (fn.__module__, fn.__name__),) + args to serialise the function for the queue. The suggestion here: https://stackoverflow.com/questions/20594193/dynamically-created-method-and-decorator-got-error-functools-partial-object-h

Is that in python 3.5 and later a reference to the original function is maintained in the partial. You can access it as .func.

benwhalley avatar May 28 '20 21:05 benwhalley

Interesting issue. Besides not knowing if this would work if the partial issue is fixed, the partial issue by itself is worth being fixed. I'm more than happy to review and merge any pull request fixing that issue. Sadly, I won't be able to provide a fix soon.

stephrdev avatar Jun 02 '20 10:06 stephrdev

I stumbled upon the same problem.

As a workaround (in my case), you can do:

class SomeJob(Job):
    lifetime = 600

    def fetch(self):
        return something()

    def key(self):
        return self.class_path

some_function = SomeJob().get()

Only downside is, is that you cannot wrap a get function from a class based view, so I went one level lower to the function that really needed some caching.

Beenhakker avatar Jun 01 '22 20:06 Beenhakker