What's the best way to serialize more than one model to json in Django 1.6?
<p>I have 3 models show as below.</p>

<pre><code>    class DocumentClass(models.Model):
        text = models.CharField(max_length=100)


    class DocumentGroup(models.Model):
        text = models.CharField(max_length=100)
        documentclass = models.ForeignKey(DocumentClass)


    class DocumentType(models.Model):
        text = models.CharField(max_length=100)
        documentgroup = models.ForeignKey(DocumentGroup)
</code></pre>

<p>And my goal is something like this:</p>

<pre><code>[
    {
        'pk': 1,
        'model': 'DocumentClass',
        'fields':{
            'text':'DocumentClass1',
            'documentgroup':
            [
                {
                    'pk': 1,
                    'model': 'DocumentGroup'
                    'field':
                    {
                        'text':'DocumentGroup1'
                    }
                }
            ]
        }
    },
    {
        'pk': 2,
        'model': 'DocumentClass',
        'fields':{
            'text':'DocumentClass2'
        }
    }
]
</code></pre>

<p>I usually serialize a model by </p>

<pre><code>jsonstr = serializers.serialize("json", DocumentType.objects.all())
</code></pre>

<p>But for my goal. I have no idea.
As the title. What is the best way to do that?</p>

<p>Edit:
The relation of above models look like:</p>

<pre><code>DocumentClass1
|-DocumentGroup1
| |-DocumentType1
| |-DocumentType2
| |-...
|-DocumentGroup2
| |-...
DocumentClass2
|-DocumentGroup...
| |-DocumentType...
</code></pre>

<p>As I follow by @nofinator's answer in "If The Models Are Related" section everything looks fine until <code>json = JSONRenderer().render(serializer.data)</code>I got above errors. How can I eliminate them?</p>

<pre><code>AttributeError: Got AttributeError when attempting to get a value for field `documenttypes` on serializer `DocumentGroupSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `DocumentGroups` instance.
Original exception text was: 'DocumentGroups' object has no attribute 'documenttypes'.
</code></pre>

<p>Here is my serialize.py</p>

<pre><code>from rest_framework import serializers
from .models import *


class DocumentTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = DocumentTypes
        fields = ('id', 'text')


class DocumentGroupSerializer(serializers.ModelSerializer):
    documenttypes = DocumentTypeSerializer(many=True)

    class Meta:
        model = DocumentGroups
        fields = ('id', 'text', 'documenttypes')


class DocumentClassSerializer(serializers.ModelSerializer):
    documentgroups = DocumentGroupSerializer(many=True)

    class Meta:
        model = DocumentClasses
        fields = ('id', 'text', 'documentgroups')
</code></pre>

<p>And this is my models.py</p>

<pre><code>from django.db import models


class DocumentClasses(models.Model):
    text = models.CharField(max_length=100)


class DocumentGroups(models.Model):
    text = models.CharField(max_length=100)
    documentclass = models.ForeignKey(DocumentClasses)


class DocumentTypes(models.Model):
    text = models.CharField(max_length=100)
    documentgroup = models.ForeignKey(DocumentGroups)
</code></pre>

<p>Update.
When I try to add <code>read_only=True</code> my serializer.py look like</p>

<pre><code>from rest_framework import serializers
from .models import *

class DocumentTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = DocumentTypes
        fields = ('id', 'text', 'documentgroup')


class DocumentGroupSerializer(serializers.ModelSerializer):
    documenttypes = DocumentTypeSerializer(many=True, read_only=True)

    class Meta:
        model = DocumentGroups
        fields = ('id', 'text', 'documenttypes')


class DocumentClassSerializer(serializers.ModelSerializer):
    documentgroups = DocumentGroupSerializer(many=True, read_only=True)

    class Meta:
        model = DocumentClasses
        fields = ('id', 'text', 'documentgroups')
</code></pre>

<p>Then try:</p>

<pre><code>queryset = DocumentClasses.objects.all()
serializer = DocumentClassSerializer(queryset, many=True)
json = JSONRenderer().render(serializer.data)
</code></pre>

<p>I got only <code>'[{"id":1,"text":"class1"}]'</code> 
But this is my expected:</p>

<pre><code>[  
   {  
      "id":1,
      "text":"class1",
      "documentgroups":[  
         {  
            "id":1,
            "text":"group1",
            "documenttypes":[  
               {  
                  "id":1,
                  "text":"type1"
               }
            ]
         }
      ]
   }
]
</code></pre>

python json serialization django-views