Note that there are some explanatory texts on larger screens.

plurals
  1. POdouble regroup in django template
    text
    copied!<p>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:</p> <p>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'):</p> <pre><code>nodes: { nodeA: {}, nodeB: {} }, edges: { nodeA: { nodeB: { measurement1: {}, measurement2: {} } } } </code></pre> <p>so my model is basically like this:</p> <pre><code>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' ) </code></pre> <p>the problem is that my template currently looks like this (which will overwrite the json :</p> <pre><code>"nodes": &lt;blah&gt; "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 %} } </code></pre> <p>which in term gives something like:</p> <pre><code>edges: { nodeA: { nodeB: { id: measurement1, service_type: service1 }, nodeB: { id: measurement2, service_type: service2 } } </code></pre> <p>so basically, i need a nested regroup statement within the <code>for item in source.list</code> 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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload