django-cacheback
django-cacheback copied to clipboard
Support for @method_decorator in Python 3.5 and later
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.
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.
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.