1

I am working with a MVC model (django). I have a for loop in the template that collects data from a sqlite3 database. I need to fill a JavaScript variable (with JSON format) that splits radar.client field from the database. This field is a comma-separated values string. I need to parse it into the json in different "children" values. Let me go into the practice now.

The code that generates the json var looks like:

var json = {
    "id": "1",
    "name": "Server",
    "children": [
      {% if clave_radar % }
        {% for radar in clave_radar % }
        {"id": "{{ radar.key }}",
         "name": "{{ radar.ap }}",
         "data": {"": "", "": ""},
         "children": [
//This is where I need to split in different children
               {"id": "1_{{ radar.key }}",
                "name": "{{ radar.clients }}",
                "data": {"": "", "": ""},
                "children": []},

         ]},
          {% endfor % }
      {% endif % }
    ],
};

Now let me show you an example of how the processed code in the server looks like for the client:

  var json_test={
    "id": "1",
    "name": "Server",
    "children": [
      {
        "id": "13",
        "name": "WLT",
        "data": {"": "","": ""},
        "children": [
          {
            "id": "1_13",
            "name": "081196(Intel Corporate), 68a3c4(Liteon Technology Corporation), b8d9ce(Samsung Electronics)",
            "data": {"": "","": ""},
            "children": []
          },
          //Children Liteon and Samsung should appear here and not packed in 1_13
        ]
      },
      ],
  };

And this is how I am struggling to make it look like:

varjson_real={
  "id": "1",
  "name": "Server",
  "children": [
    {
      "id": "13",
      "name": "WLT",
      "data": {"": "","": ""},
      "children": [
        {
          "id": "1_13",
          "name": "081196(Intel Corporate)",
          "data": {"": "","": ""},
          "children": []
        },
        {
          "id": "2_13",
          "name": "68a3c4(Liteon Technology Corporation)",
          "data": {
            "": "",
            "": ""
          },
          "children": []
        },
        {
          "id": "3_13",
          "name": "b8d9ce(Samsung Electronics)",
          "data": {"": "","": ""},
          "children": []
        },
      ]
    },  
  ]
};
EnriqueH
  • 161
  • 3
  • 13

2 Answers2

3

Why you don't loop over clients, like this:

var json = {
"id": "1",
"name": "Server",
"children": [
{% if clave_radar % }
    {% for radar in clave_radar % }
        {"id": "{{ radar.key }}",
         "name": "{{ radar.ap }}",
         "data": {"": "", "": ""},
         "children": [
         {% for client in radar.clients %}
              {"id": "1_{{ radar.key }}",
               "name": "{{ client }}",
               "data": {"": "", "": ""},
               "children": []},
         {% endfor % }
         ]},
    {% endfor % }
{% endif % }
],
};
  • I have tried this way and it will go over a new loop (the `for` loop you created) for each character at "081196(Intel Corporate), 68a3c4(Liteon Technology Corporation), b8d9ce(Samsung Electronics)" and I need that `for` to happen 3 times in this particular case (comma separation). – EnriqueH Feb 23 '14 at 17:37
  • I didn't know it is a string, anyway you can change forloop to this: {% for client in radar.clients.split(', ') %} this why you will split this string to 3 objects. – Łukasz Staniszewski Feb 23 '14 at 17:58
  • You are helping me getting closer to the solution... I can not use `.split` inside a for in django. Nevertheless, I am now reading how to split from the model at http://stackoverflow.com/questions/8317537/django-templates-split-string-to-array – EnriqueH Feb 23 '14 at 18:34
0

I just found the way of solving this problem. First I got inspired by this how to split a string to an array with django

I added this function to the radar model:

def list_clients(self):
    return self.clientes_encontrados.split('), ')

Then, the code would like to something like:

var json_radar = {
    "id": "1",
    "name": "Server",
    "children": [
    {% if clave_radar %}
      {% for radar in clave_radar %}
        {"id": "{{ radar.key }}",
        "name": "{{ radar.ap }}",
        "data": {"": "","": ""},
        "children": [
          {% if radar.clients != "null" %}
            {% for client in radar.list_clients %}
          {"id": "1_{{ radar.key }}",
           "name": "{{ client }}",
           "data": {"": "", "": ""},
           "children": []},
            {% endfor %}
          {% endif %} 

        ]},
        {% endfor %}
    {% endif %}  
    ],
};
Community
  • 1
  • 1
EnriqueH
  • 161
  • 3
  • 13