django 模板中的双重重组
我有一些来自 django 视图的输出,需要进行一些排序。我希望我可以在模板中而不是在代码中执行此操作:
基本上,我需要表示节点/边图;但是,两个节点之间可能有很多边(有向) - 因此我想在它们之间创建唯一的 id,以便我的 json 不会被覆盖(“measurement1”和“measurement2”):
nodes: {
nodeA: {},
nodeB: {}
},
edges: {
nodeA: {
nodeB: {
measurement1: {},
measurement2: {}
}
}
}
所以我的模型基本上是这样的:
class Measurement( models.Model ):
service = models.ForeignKey( Service, db_index=True )
source_node = models.ForeignKey( Node, related_name='source' )
target_node = models.ForeignKey( Node, related_name='target' )
问题是我的模板目前看起来像这样(它将覆盖 json : ,
"nodes": <blah>
"edges": {
{% regroup measurements by source_node_id as source_list %}
{% for source in source_list %}
"{{ source.grouper }}": {
{% for item in source.list %}
"{{ item.target_node_id }}": {
"id": "{{ item.id }}",
"service_type": "{{ item.service.service_type|lower }}" }{% if not forloop.last %},{% endif %}
{% endfor %}
}{% if not forloop.last %},{% endif %}
{% endfor %}
}
这在术语中给出了类似的内容:
edges: {
nodeA: {
nodeB: {
id: measurement1,
service_type: service1
},
nodeB: {
id: measurement2,
service_type: service2
}
}
所以基本上,我需要在 for item in source.list
中嵌套一个重组语句与该循环中的特定源和目标节点相匹配的测量;但我似乎无法让它发挥作用?
i have some output from a django view that requires some sorting. i'm hoping i can do this in the template rather in in code:
basically, i need to represent a node/edge graph; however, there may be many edges between two nodes (directed) - therefore i want to create unique id's between them so that my json doesn't get overwritten ('measurement1' and 'measurement2'):
nodes: {
nodeA: {},
nodeB: {}
},
edges: {
nodeA: {
nodeB: {
measurement1: {},
measurement2: {}
}
}
}
so my model is basically like this:
class Measurement( models.Model ):
service = models.ForeignKey( Service, db_index=True )
source_node = models.ForeignKey( Node, related_name='source' )
target_node = models.ForeignKey( Node, related_name='target' )
the problem is that my template currently looks like this (which will overwrite the json :
"nodes": <blah>
"edges": {
{% regroup measurements by source_node_id as source_list %}
{% for source in source_list %}
"{{ source.grouper }}": {
{% for item in source.list %}
"{{ item.target_node_id }}": {
"id": "{{ item.id }}",
"service_type": "{{ item.service.service_type|lower }}" }{% if not forloop.last %},{% endif %}
{% endfor %}
}{% if not forloop.last %},{% endif %}
{% endfor %}
}
which in term gives something like:
edges: {
nodeA: {
nodeB: {
id: measurement1,
service_type: service1
},
nodeB: {
id: measurement2,
service_type: service2
}
}
so basically, i need a nested regroup statement within the for item in source.list
for just the measurements that match that particular source and target nodes within that loop; but i can't seem to get it to work. any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我倾向于完全不使用重组来做到这一点。请记住,重新组合需要首先对您的列表进行排序,我假设您的列表已经在两个级别上进行了排序。鉴于此,您可以使用 ifchanged 执行类似以下操作来跟踪源节点和/或目标节点自上一次循环迭代以来是否已更改。您可能需要修剪一些空格以产生更漂亮的输出:
I would be inclined to do it without using regroup at all. Keeping in mind that regroup requires your list to be ordered to begin with, I assume that your list is already ordered at both levels. Given that, you could do something like the following using ifchanged to keep track of whether the source and/or target node has changed since the previous loop iteration. You'll probably want to trim some whitespace in order to produce prettier output: