Performing calculations on Django database values: views.py, template or Javascript? <p>I have a Django project that interfaces with a PostgreSQL database; one of my tables is about 450 rows long and each row contains about a dozen columns.</p> <p>Each column's value must be put into a different formula to produce the final data I want to display in the template layer, but each row will be processed with the same set of calculations. (In other words, I want to perform 12 different calculations 450 times each.)</p> <p>My question is, of the following methods, which is the generally accepted method of doing so, and which is the best in terms of performance:</p> <p>A) Write each calculation as a model method in models.py, query all objects of interest (again, about 450 of them) in views.py and pass them directly to the template, and then use Django's template language to write things like:</p> <pre><code>&lt;table&gt; {% for item in list %} &lt;tr&gt; &lt;td&gt;{{item.name}}&lt;/td&gt; ... &lt;/tr&gt; {% endfor %} &lt;/table&gt; </code></pre> <p>(where <code>.name</code> is a model method)</p> <p>B) Peform the query <em>and</em> calculations in the views.py file and pass the results organized into one big dictionary or list or JSON to the template (and then parse it using template tags),</p> <p>C) Pass all of the objects of interest to the template as in A), but perform calculations on each object's fields using Javascript or jQuery,</p> <p>D) Combine B) and C) to perform all database queries in views.py and then pass the <em>raw</em> values as a big dictionary/list/JSON object to the template and perform calculations on those values using Javascript/jQuery.</p> <p>To my mind, method A) seems the least efficient as it requires you to hit the database a ton of times in the template layer. But it makes writing the view and template extremely simple (just requires you to write 12 different model methods). Method B) seems to follow the general tenet of performing as much logic as possible in your view, and simply passing along the final results to be displayed in the template. But it makes the view function long and ugly. Methods C) and D) put most of load on the end user's browser, which seems like it could really take a load off the server, but then obviously won't work if the user has JS turned off.</p> <p>But again, I'd like to know if there's an accepted best practice for this kind of situation, and whether that contradicts the <em>fastest</em> method from a computational standpoint.</p> <p>And obviously, if I've missed the best method of all, please let me know what it would be.</p>