如何迭代对象的字段值?
在 django 项目中,我尝试显示这样的报告:
Heading: Field1 Field2 Field3 of Type A Field3 of Type B
Data: Record1Value1 Record1Value2 Record1Value3
Record2Value1 Record2Value2 Record2Value3
Record3Value1 Record3Value2 Record3Value3
其中 Record1Value1 和 Record2Value1、Record3Value1 属于 value1 列表。它是模型中对象的字段值列表。与列表值2相同。 Record1Value3 和 Record3Value3 属于 value3 列表,但按 type='type A' 进行过滤。 Record2Value3属于values3列表,按type='type B'过滤。所以在我的views.py中,我的代码如下:
objects=model.objects.filter(field=input,…)
values1=objects.values_list(field1, flat=True)
values2=objects.values_list(field2, flat=True)
objects3a= objects.filter(type=’type A’)
objects3b= objects.filter(type=’type B’)
values3a=objects3a.values_list(field3, flat=True)
values3b=objects3b.values_list(field3, flat=True)
functions=objects.values_list(type,flat=True)
在html页面中,我使用以下代码:
<th>
<td> field1</td> <td> field2</td> <td> field3 of type A</td> <td>field3 of typeB </td>
</th>
{%for value1 in values1%}
{%for value2 in values2%}
{%for value3a in values3a%}
{%for value3b in values3b%}
<tr>
<td> {{value1}}</td> <td> {{value2}}</td>
<td>
{%if function==’type A’%}
{{value3a}}
{%endif%}
</td>
<td>
{%if function==’type B’%}
{{value3b}}
{%endif%}
</td>
</tr>
{%endfor%}
{%endfor%}
{%endfor%}
{%endfor%}
我想要做的是迭代每条记录并将每条记录的字段值放入相应的单元格中,但是对于field3,如果记录的“类型”字段为“A 类型”,然后将 field3 值放入“A 类型的 field3”列中,并将“B 类型的 field3”留空。反之亦然,类型 B。
问题是它并没有真正以这种方式循环每个记录。代码块:
{%if function==’type A’%}
{{value3a}}
{%endif%}
并没有真正起作用,因为它无法识别 function 和 value3a 属于同一个对象:objects3a。我必须编写 forms.py 才能做到这一点吗?如果是这样,怎么办? (请注意,我无法在 forms.py 中声明对象,因为我必须在views.py 中获取用户输入)。
另外,有什么办法可以简化代码吗?如此多的 for 循环使得加载数据几乎不可能。
希望有人能帮助我!
In a django project I am trying to display the report like this:
Heading: Field1 Field2 Field3 of Type A Field3 of Type B
Data: Record1Value1 Record1Value2 Record1Value3
Record2Value1 Record2Value2 Record2Value3
Record3Value1 Record3Value2 Record3Value3
Of which Record1Value1 and Record2Value1, Record3Value1 belong to a list of values1. It is a list of field values of objects in model. The same with list values2. Record1Value3 and Record3Value3 belong to a list of values3, but filtered by type=’type A’. Record2Value3 belong to a list of values3, filtered by type=’type B’. So in my views.py I code like below:
objects=model.objects.filter(field=input,…)
values1=objects.values_list(field1, flat=True)
values2=objects.values_list(field2, flat=True)
objects3a= objects.filter(type=’type A’)
objects3b= objects.filter(type=’type B’)
values3a=objects3a.values_list(field3, flat=True)
values3b=objects3b.values_list(field3, flat=True)
functions=objects.values_list(type,flat=True)
In the html page, I use the following codes:
<th>
<td> field1</td> <td> field2</td> <td> field3 of type A</td> <td>field3 of typeB </td>
</th>
{%for value1 in values1%}
{%for value2 in values2%}
{%for value3a in values3a%}
{%for value3b in values3b%}
<tr>
<td> {{value1}}</td> <td> {{value2}}</td>
<td>
{%if function==’type A’%}
{{value3a}}
{%endif%}
</td>
<td>
{%if function==’type B’%}
{{value3b}}
{%endif%}
</td>
</tr>
{%endfor%}
{%endfor%}
{%endfor%}
{%endfor%}
What I want to do is iterate over each record and put the field values of each record in corresponding cell, but for field3, if the ‘type’ field of the record is ‘type A’ then put the field3 value in the column ‘field3 of type A’, and leave ‘field3 of type B’ blank. Vice versa to type B.
The problem is that it is not really looping over each record this way. The block of codes:
{%if function==’type A’%}
{{value3a}}
{%endif%}
does not really work because it does not recognize function and value3a belong to the same object: objects3a. Do I have to write a forms.py to do this? If so, how? (Note that I can not declare objects in forms.py because I have to get user input in views.py).
Also, is there any way to simplify the codes? These many forloops makes it almost impossible to load the data.
Hope someone could help me out!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有模型实现的所有细节,很难确定,但您应该能够将
objects
结果直接传递到模板并在那里使用它。想法是这样的:
It's hard to say for sure without all of the details of your model implementation, but you should be able to pass the
objects
result directly to your template and work with it there.Here's the idea: