Note that there are some explanatory texts on larger screens.

plurals
  1. PONewbie: Django : Adding calculated results to Queryset before passing to template
    text
    copied!<p>Its day two of my new life with Django, please excuse the simplicity of my question.</p> <p>I have an existing DB table(read-only access) that I have successfully displayed the contents of on a webpage using urls, views, models and all that good stuff.</p> <p>The challenge I have is the table does not contain all the information I need to display. The table contains test results with the columns, sampletime, samplevalue, sampleresult. I need to display different data based on what I calculate from those columns.</p> <p>My end goal is to display this info as a time series graph using <a href="http://code.google.com/p/flotr/" rel="noreferrer">flotr</a>. For now Id be happy to just dump the data I need to a table on a web page.(So I can visualize the resulting data)</p> <p>What Id like to pass to the template is,</p> <ul> <li>jssampletime(the sampletime datetime object converted to javascript epoch ms)</li> <li>resultvalue(rolling sum+- of samplevalue based on whether sampleresult was good or bad)</li> </ul> <p>I'm fine with creating jssampletime and resultvalue using def functions. I presume I would add these functions to views.py</p> <p>I guess what I need to do iterate over the a querySet in views.py and store the results in a list of dictionaries which I pass to the template. Something like this(code not tested).</p> <p>views.py</p> <pre><code># views.py # Sudo code to assit in asking the question from django.shortcuts import render_to_response from thing.reporter.models import Samples def _datetime_to_js(sampletime): #.. date conversion epoch magic return jsd_result def _rolling_sum(samplevalue,sampleresult): #.. summing magic return sum_result def dumptable(request): # The def that is called by urls.py object_list = Samples.objects.all() list_for_template = [] for row in object_list: jssampletime = _datetime_to_js(row.sampletime) resultvalue = _rolling_sum(row.samplevalue,row.sampleresult) list_for_template.append({'jssampletime':jssampletime,'resultvalue':resultvalue}) return render_to_response('tabledump.html', {'result_list': list_for_template}) </code></pre> <p>tabledump.html</p> <pre><code># tabledump.html template {% block content %} &lt;h2&gt;Results dumped to page for testing&lt;/h2&gt; &lt;ul&gt; &lt;table&gt; {% for result in result_list %} &lt;tr&gt; &lt;td&gt;{{ result.jssampletime }}&lt;/td&gt; &lt;td&gt;{{ result.resultvalue }}&lt;/td&gt; &lt;/tr&gt; {% endfor %} &lt;/table&gt; &lt;/ul&gt; {% endblock %} </code></pre> <p>I think this would work but Im not sure if it is the Django MVC way.</p> <p>Is it right that I,</p> <ul> <li>calculate the result I need in views.py by interating over a queryset result?</li> <li>pass my result to a template as a list of dict(is a queryset more than that)?</li> </ul> <p>I guess Im looking for some direction and code tips. Am I on the right path ? Is there a better way ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload