1👍
✅
This isn’t the best way to design that API, because what you’re asking for is the use of eval()
, and you don’t really want that because eval()
is evil and coding an application around eval isn’t safe.
The better approach would be this:
def myobject_cache(key, query, time):
data = cache.get(key)
if data:
return data
else:
result = list(CustomObjectThatHoldsLotsOfQuerySets.get(query))
cache.set(key, result, time)
return result
class CustomObjectThatHoldsLotsOfQuerySets(object):
def get(key):
if key == "MyObject":
return MyObject.objects.filter(x=y)
#do other stuff here
Source:stackexchange.com