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>

<h2>If The Models Are Related</h2>

<p>This will require some more customized serialization, for which you will have to use <a href="http://www.django-rest-framework.org/api-guide/relations/#nested-relationships" rel="nofollow">Django Rest Framework's serializers</a>.</p>

<p>Try subclassing <code>ModelSerializer</code>:</p>

<pre><code>class DocumentTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = DocumentType


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

    class Meta:
        model = DocumentGroup


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

    class Meta:
        model = DocumentClass


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

<h2>If The Models Are Not Related</h2>

<p>According to the <a href="https://docs.djangoproject.com/en/1.10/topics/serialization/#serializing-data" rel="nofollow" title="Django docs">Django docs</a>, the 2nd argument to <code>serialize()</code> can be "any iterator that yields Django model instances".</p>

<p>Now you'll need to pass your 3 kinds of instances as an iterator. It seems like Python's <code>itertools.chain</code> is the preferred method, as voted <a href="http://stackoverflow.com/a/434755/245915">here</a>.</p>

<p>So your call would look something like:</p>

<pre><code>instances = list(chain(DocumentClass.objects.all(), DocumentGroup.objects.all(), DocumentType.objects.all())
jsonstr = serializers.serialize("json", instances)
</code></pre>

python json serialization django-views