title: use json data in django template
body: <p>I have a model that has one class that is made up of a number of others using OneToOneField.  <strong>My ultimate goal is to be able to loop over a data structure in an abstract way (without using field names) and output the contents to my template using template tags.</strong>  I would like to provide my template with nested data in json format and parse the json using template tags directly in my template.  I have made some use of the Django rest framework.  The following is a generic example of my codebase:</p>
  
  <pre><code>
  models.py--------------------------------------
  
  from django.db import models
  from rest_framework import serializers
  
  
  class Class1(models.Model):
      class_1_field_1 = models.CharField()
      class_1_field_2 = models.CharField()
  
  
  class Class2(models.Model):
      class_2_field_1 = models.CharField()
      class_2_field_2 = models.CharField()
  
  
  class MainClass(models.Model):
      field_1 = models.CharField()
      field_2 = models.IntegerField()
      field_3 = models.DateTimeField()
  
      class_1 = models.OneToOneField(Class1, on_delete=models.CASCADE,)
      class_2 = models.OneToOneField(Class2, on_delete=models.CASCADE,)
  
  
  class Class1Serializer(serializers.ModelSerializer):
      class Meta:
          model = Class1
          fields = ('class_1_field_1', 'class_1_field_2')
  
  
  class Class2Serializer(serializers.ModelSerializer):
      class Meta:
          model = Class2
          fields = ('class_2_field_1', 'class_2_field_2')
  
  
  class MainClassSerializer(serializers.ModelSerializer):
      class_1= Class1Serializer()
      class_2= Class2Serializer()
  
      class Meta:
          model = MainClass
          fields = ('field1', 'field2', 'field3', 'class_1', 'class_2')
  
  
  views.py-------------------------------------
  
  from django.views.generic import ListView
  from rest_framework.renderers import JSONRenderer
  from MyApp.models import MainClass, MainClassSerializer
  
  
  MainClassListView(ListView):
      context_object_name = 'mainclasses'
      model = MainClass
      serializer_class = MainClassSerializer
  
      def get_queryset(self):
          object_list = MainClass.objects.filter(user__in=[self.request.user])
          return object_list
  
      def get_context_data(self, **kwargs):
          context = super().get_context_data(**kwargs)
          serializer = MainClassSerializer(MainClass.objects.filter(user__in=[self.request.user]), many=True)
          context['data'] = JSONRenderer().render(serializer.data)
          return context
  
      def dispatch(self, request, *args, **kwargs):
          return super(MainClassListView, self).dispatch(request, *args, **kwargs)
  
  
  index.html----------------------------------------------------
  
  ...
  
  {{data}}
  
  ...
  
  json output-------------------------------------------------------
  
  b'[{
     "field_1": "Some String",
     "field_2": 100,
     "field_3": "2020-04-20T02:34:27.912451Z",
     "class_1": {
        "class_1_field_1": "str_1",
        "class_1_field_2": "str_2"
     },
     "class_2": {
        "class_2_field_1": "str_3",
        "class_2_field_2": "str_4"
     }
  }]'
  
  </code></pre>
  
  <p>The <code>{{data}}</code> tag displays a string (see above) with the following format: <code>b'[valid_json_is_displayed_here]'</code>, where the <code>valid_json_is_displayed_here</code> is a properly formatted json structure that includes the nested data from the OneToOneField relationships.</p>
  
  <p>I would like to parse the individual values in the returned json output in template tags directly in my template including the one-to-one relationship data.  Is this possible in Django and how can I accomplish this?  I feel like it should be simple, what am I missing?  </p>
  
  <p>Thank you.</p>