django 模板中的双重重组

发布于 2024-11-04 19:42:47 字数 1416 浏览 1 评论 0原文

我有一些来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2024-11-11 19:42:47

我倾向于完全不使用重组来做到这一点。请记住,重新组合需要首先对您的列表进行排序,我假设您的列表已经在两个级别上进行了排序。鉴于此,您可以使用 ifchanged 执行类似以下操作来跟踪源节点和/或目标节点自上一次循环迭代以来是否已更改。您可能需要修剪一些空格以产生更漂亮的输出:

edges: {
{% for meas in measurements %}
  {% ifchanged meas.source_node_id %}
    {% if not forloop.first %}
        }
      },
    {% endif %}
    {{ meas.source_node_id }}: {
      {{ meas.target_node_id }}: {
  {% else %}
    {% ifchanged meas.target_node_id %}
      },
      {{ meas.target_node_id }}: {
    {% else %}
      ,
    {% endifchanged %}
  {% endifchanged %}
  measurement{{ meas.id }}: { }
  {% if forloop.last %}
    }
  }
  {% endif %}
{% endfor %}
}

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:

edges: {
{% for meas in measurements %}
  {% ifchanged meas.source_node_id %}
    {% if not forloop.first %}
        }
      },
    {% endif %}
    {{ meas.source_node_id }}: {
      {{ meas.target_node_id }}: {
  {% else %}
    {% ifchanged meas.target_node_id %}
      },
      {{ meas.target_node_id }}: {
    {% else %}
      ,
    {% endifchanged %}
  {% endifchanged %}
  measurement{{ meas.id }}: { }
  {% if forloop.last %}
    }
  }
  {% endif %}
{% endfor %}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文