在Django模型中循环时如何获取下一个obj [英] How to get the next obj when looping in the django model
问题描述
这是代码:
{% for o in page_obj.object_list %}
<tr style="color:#4A3C8C;background-color:{% cycle '#E7E7FF' '#F7F7F7' %};">
<td align="center"> {{o.terminal_id}}</td>
<td align="center"> {{o.time_stamp}}</td>
<td align="center" class="lat"> {{o.latitude|floatformat:"6"}}</td>
<td align="center" class="lng"> {{o.longitude|floatformat:"6"}}</td>
<td align="center"> {{o.speed}}</td>
<td align="center">
<script>
$("script").last().parent().parent()
</script>
</td>
<td align="center"> {{o.speed}}</td>
<td align="center"> {{o.adress_reality}}</td>
</tr>
{%endfor%}
我想获得'o'
对象之后的下一个对象.
如何从'o'
对象获取下一个?或者,如何在python文件中获取下一个对象,以便可以在html中显示此对象,如下所示?
I want to get the next object after 'o'
object.
How do I get the next from the 'o'
object? Alternatively, how to get the next object in the python file that i can show this in the html as shown below?
{{ o.next_obj }}
谢谢
推荐答案
您尝试使用get_next_by_FOO吗? https://docs.djangoproject. com/zh-CN/1.2/ref/models/instances/#django.db.models.Model.get_next_by_FOO
did you try to use get_next_by_FOO ? https://docs.djangoproject.com/en/1.2/ref/models/instances/#django.db.models.Model.get_next_by_FOO
如果不是实时生成字段,则可以(自然)添加到模型中:
If the field is not generated on the fly, you could add to your model (naively):
class MyObj(models.Model):
lat = ...
lon = ...
def get_next_by_id(self):
return self.objects.get(id=self.id+1)
并使用{{ o.get_next_by_id.lat }}
否则, templatetags 很好用,请参见标签部分,但需要更多代码. 如果您向我提供了您正在做的事情的更详细的示例,我可以尝试为您提供一个通用的模板标签,该标签可能适用于您的情况.
Otherwise it is a good use for the templatetags, see the tag section but require more code. If you provide me a more detailed example of what you are doing i could try to give you a generic templatetag which might work in your case.
祝你好运.
这篇关于在Django模型中循环时如何获取下一个obj的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!