新手:Django:在传递给模板之前,将计算结果添加到Queryset [英] Newbie: Django : Adding calculated results to Queryset before passing to template
问题描述
我有一个现有的DB表(只读访问),我的已经使用网址,视图,模型和所有好的东西成功地显示在网页上的内容。
我遇到的挑战是不包含所有信息需要显示。该表包含列,sampletime,samplevalue,sampleresult的测试结果。我需要根据我从这些列中计算的数据显示不同的数据。
我的最终目标是使用 flotr 。现在,Id很乐意将我需要的数据转储到网页上的一个表(所以我可以可视化所得到的数据)
什么ID要传递给模板是
- jssampletime(sampletime datetime
对象转换为javascript epoch
ms) - resultvalue(滚动和+ - 基于sampleresult是否好的抽样值)
我很好使用def函数创建jssampletime和resultvalue。我想我会将这些函数添加到views.py
我想我需要在views.py中迭代查询集,并将结果存储在我传给模板的字典这样的东西(代码未测试)。
views.py
#views.py
#Sudo代码来解决问题
从django.shortcuts import render_to_response
from thing.reporter.models import Samples
def _datetime_to_js (sampletime):
#..日期转换epoch magic
return jsd_result
def _rolling_sum(samplevalue,sampleresult):
#.. summing magic
return sum_result
def dumptable(request):#由urls.py
调用的def object_list = Samples.objects.all()
list_for_template = [ ]
对于object_list中的行:
jssampletime = _datetime_to_js(row.sampletime)
resultvalue = _rolling_sum(row.samplevalue,row.sampleresult)
list_for_template.append({'jssampletime' :jssampletime,'resultvalue':resultvalue})
返回render_to_response('tabledump.html',{'result_list':list_for_templa te})
tabledump.html
#tabledump.html template
{%block content%}
< h2>结果转储到页面进行测试< / h2>
< ul>
< table>
{result for result_list%}
< tr>
< td> {{result.jssampletime}}< / td>
< td> {{result.resultvalue}}< / td>
< / tr>
{%endfor%}
< / table>
< / ul>
{%endblock%}
我认为这会工作,但我不知道是否Django MVC的方式。
是的,我,
- 结果我需要在views.py中通过查询查询结果进行交互?
- 将我的结果传递给一个模板作为
的列表(是一个查询超过
那个)?
我想我在寻找一些方向和代码提示。
我在正确的路上吗?有没有更好的方法?
如果您显示的信息在一个模型中,为什么不只是添加属性/方法模型显示您需要摆脱的任何信息?然后,您可以将实际的模型列表/查询集传递给您的模板,并将方法称为属性。
例如
class MyModel(models.Model):
model_field = models.CharField(max_length = 255)
@property
def calculate_field(自我):
return self._do_calculation(self.model_field)
如果您需要访问状态变量在你的循环中,不要忘记,你可以将任何属性附加到一个python对象。这可以是非常有用的。所以在你看来,你可能会有这样的东西:
for object in object_list:
#update some variable we want to在模板中使用
row.newly_added_field = run_calculation(row,somevariable)
可以在模板中访问:
{%for result in result_list%}
< tr>
<! - 一些直接显示模型的东西 - >
< td> {{result.calculated_field}}< / td>
< td> {{result.newly_added_field}}< / td>
< / tr>
{%endfor%}
Its day two of my new life with Django, please excuse the simplicity of my question.
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.
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.
My end goal is to display this info as a time series graph using flotr. 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)
What Id like to pass to the template is,
- jssampletime(the sampletime datetime object converted to javascript epoch ms)
- resultvalue(rolling sum+- of samplevalue based on whether sampleresult was good or bad)
I'm fine with creating jssampletime and resultvalue using def functions. I presume I would add these functions to views.py
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).
views.py
# 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})
tabledump.html
# tabledump.html template
{% block content %}
<h2>Results dumped to page for testing</h2>
<ul>
<table>
{% for result in result_list %}
<tr>
<td>{{ result.jssampletime }}</td>
<td>{{ result.resultvalue }}</td>
</tr>
{% endfor %}
</table>
</ul>
{% endblock %}
I think this would work but Im not sure if it is the Django MVC way.
Is it right that I,
- calculate the result I need in views.py by interating over a queryset result?
- pass my result to a template as a list of dict(is a queryset more than that)?
I guess Im looking for some direction and code tips. Am I on the right path ? Is there a better way ?
If the information you are displaying is in a model why not just add properties/methods to the model to display whatever information you need to get out of it? Then you can pass the actual model list / query set to your template and call the methods as properties.
e.g.
class MyModel(models.Model):
model_field = models.CharField(max_length=255)
@property
def calculated_field(self):
return self._do_calculation(self.model_field)
If you need access to state variables in your loop, don't forget that you can attach any property to a python object. This can be extremely useful. So in your view you could have something like:
for row in object_list:
# update some variable we want to use in the template
row.newly_added_field = run_calculation(row, somevariable)
Both of these could then be accessed within the template:
{% for result in result_list %}
<tr>
<!-- some stuff that displays the model directly -->
<td>{{ result.calculated_field}}</td>
<td>{{ result.newly_added_field}}</td>
</tr>
{% endfor %}
这篇关于新手:Django:在传递给模板之前,将计算结果添加到Queryset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!