26.3.3 Caching Properties

One of the easiest optimizations is to cache a database query as a property on a model. Take, for instance, the published_posts() method on Startup. In our startup_detail.html template, we call the method seven times: the first four times changes the layout of the template, while the last two are for printing the list of published blog posts. This method currently results in multiple separate calls to the database for exactly the same information. By caching the results of the first database query, we can avoid the rest entirely.

If you browse the development server to /startup/simple-robots/, you’ll be able to click on the debug-toolbar on the right, and then the SQL button, to reveal that our site currently makes 11 ...

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.