The general Python pattern for preventing round-robin imports is to put one set of imports inside dependent functions:
# module_a.py import module_b def foo(): return "bar" def bar(): return module_b.baz()
As for caching, it looks like you need a function that looks something like this:
def get_cached(model, **kwargs): timeout = kwargs.pop('timeout', 60 * 60) key = '%s:%s' % (model, kwargs) result = cache.get(key) if result is None: result = model.objects.get(**kwargs) cache.set(key, result, timeout) return result
Now you do not need to create "getbyid" methods for each of your models. You can do this instead:
blog_entry = get_cached(BlogEntry, pk = 4)
You can write similar functions to work with full QuerySets instead of individual model objects using the .get () method.
source share