Mohamed Feddad

30%
Flag icon
>>>recent = list(posts)+list(comments)     >>>sorted(recent, key=lambda e: e.modified, reverse=True)[:3]     [<Post: user: Post1>, <Comment: user: Comment1>, <Post: user: Post0>]       Unfortunately, this operation has evaluated both the lazy QuerySet objects. The combined memory usage of the two lists can be overwhelming. Besides, it can be quite slow to convert large QuerySets into lists. A much better solution uses iterators to reduce the memory consumption. Use the itertools.chain method to combine multiple QuerySets as follows:     >>> from itertools import chain     >>> recent = ...more
Django Design Patterns and Best Practices: Industry-standard web development techniques and solutions using Python, 2nd Edition
Rate this book
Clear rating
Open Preview