+class LazyQueryList(object):
+ def __init__(self, model, items):
+ self.model = model
+ self.items = items
+
+ def __getitem__(self, k):
+ return self.model.objects.get(id=self.items[k])
+
+ def __iter__(self):
+ for id in self.items:
+ yield self.model.objects.get(id=id)
+
+ def __len__(self):
+ return len(self.items)
+
+class CachedQuerySet(models.query.QuerySet):
+ def lazy(self):
+ if len(self.query.extra) == 0:
+ return LazyQueryList(self.model, list(self.values_list('id', flat=True)))
+ else:
+ return self
+
+ def obj_from_datadict(self, datadict):
+ obj = self.model()
+ obj.__dict__.update(datadict)
+ return obj