title: how to Django dumpdata nested models in yaml
body: <p>So I have this awesome Django app which gives me satisfaction and such.
  But now the problem, I want to use <code>dumpdata</code> (or something that does the same) to export a model with a nested other model in yaml format.</p>
  
  <p>Lets say I have two models, <code>Project</code> and <code>Questions</code>. Each <code>Project</code> can have it's owns set of <code>Questions</code>.</p>
  
  <p>The code looks something like this:</p>
  
  <p>Project Model:</p>
  
  <pre><code>class Projects(SortableMixin):
      """
      Some docstring
      """
      slug = models.SlugField(
              _('slug'),
              help_text=_('Short name to address this projects from templates.'))
      # Basic fields
      object_id = models.PositiveIntegerField(blank=True, null=True)
      name = models.TextField(_('name'), help_text=_('The question.'))
      # Some other fields...
      question = models.ForeignKey(Question, null=True, blank=True)
  </code></pre>
  
  <p>Questions Model:</p>
  
  <pre><code>class Question(SortableMixin):
      """
      Some docstring
      """
      slug = models.SlugField(
          _('slug'),
          help_text=_('Short name to address this question from templates.'))
      # Basic fields
      object_id = models.PositiveIntegerField(blank=True, null=True)
      name = models.TextField(_('name'), help_text=_('The question.'))
      input = models.TextField()
  </code></pre>
  
  <p>The <code>Project</code> model has his own app and so has the <code>Questions</code>.
  The structure looks like this:</p>
  
  <pre><code>- Django
    - Apps
      - Project
      - Questions
  </code></pre>
  
  <p>Whenever I want to export my database I'll do the following:</p>
  
  <pre><code>./manage.py dumpdata --indent 4 --format yaml &gt; dbdump.yaml
  </code></pre>
  
  <p>Although this works, and I can later import it with <code>LoadData</code>, its not what I want, the output of the yaml file looks crappy. I want to have a nice nested model looking yaml file for review purposes, below the crappy looking file:</p>
  
  <p>Projects section:</p>
  
  <pre><code>-   model: project.projects
      pk: 1
      fields: {slug: "slugproject1", object_id: 10, name: "some project 1", question: ["slugquestion1"]}
  -   model: project.projects
      pk: 2
      fields: {slug: "slugproject2", object_id: 11, name: "some project 2", question: ["slugquestion2"]}
  -   model: project.projects
      pk: 3
      fields: {slug: "slugproject3", object_id: 12, name: "some project 3", question: ["slugquestion3"]}
  </code></pre>
  
  <p>Questions section:</p>
  
  <pre><code>-   model: question.question
      pk: 1
      fields: {slug: "slugquestion1", object_id: 100, name: "some question 1", input: "q1"}
  -   model: question.question
      pk: 1
      fields: {slug: "slugquestion2", object_id: 200, name: "some question 2", input: "q2"}
  -   model: question.question
      pk: 1
      fields: {slug: "slugquestion3", object_id: 300, name: "some question 3", input: "q3"}
  </code></pre>
  
  <p>What I really want is to export the yaml file like so:</p>
  
  <pre><code>-   model: project.projects
      pk: 1
      fields: {
          slug: "slugproject1", 
          object_id: 10, 
          name: "some project 1", 
          questions: {
              model: question.question
              pk: 1
              fields: {
                   slug: "slugquestion1"
                   object_id: 100
                   name: "some question 1"
                   input: "q1"
              }            
          }
      }
  -   model: project.projects
      pk: 2
      fields: {
          slug: "slugproject2", 
          object_id: 11, 
          name: "some project 2", 
          questions: {
              model: question.question
              pk: 2
              fields: {
                   slug: "slugquestion2"
                   object_id: 200
                   name: "some question 2"
                   input: "q2"
              }            
          }
      }
  -   model: project.projects
      pk: 3
      fields: {
          slug: "slugproject3", 
          object_id: 13, 
          name: "some project 3", 
          questions: {
              model: question.question
              pk: 3
              fields: {
                   slug: "slugquestion3"
                   object_id: 300
                   name: "some question 3"
                   input: "q3"
              }            
          }
      }
  </code></pre>
  
  <p>To achieve this I implemented a custom serialiser inside projects:</p>
  
  <pre><code>- Django
    - Apps
      - Project
        - Management
          - Commands
            - test.py
      - Questions
  </code></pre>
  
  <p>The code looks like:</p>
  
  <pre><code>from django.core.management.base import BaseCommand, CommandError
  from apps.project import Projects
  from apps.questions import Question
  from rest_framework import serializers
  import yaml
  
  
  class QuestionSerialier(serializers.ModelSerializer):
      class Meta:
          model = Question
          fields = ('pk', 'slug', 'object_id', 'name', 'input')
  
  
  class ProjectsSerializer(serializers.ModelSerializer):
      questions = QuestionSerialier(many=True, read_only=True)
  
      class Meta:
          model = Projects
          fields = ('pk', 'slug','object_id', 'name', 'questions')
  
  
  class Command(BaseCommand):
      help = ''
  
      def add_arguments(self, parser):
          pass
  
      def handle(self, *args, **options):
  
          with open('result.yaml', 'w') as yaml_file:
              for i in Projects.objects.filter():
                  yaml.dump(ProjectsSerializer(i).data, 
                                  yaml_file,
                                  default_flow_style=False,
                                  allow_unicode=False,
                                  encoding=None)
  </code></pre>
  
  <p>I can run the code by running:</p>
  
  <pre><code>./manage.py test
  </code></pre>
  
  <p>Only this exports my models like so:</p>
  
  <pre><code>- project: 1
    pk: 1
    slug: "slugproject1"
    object_id: 10
    name: "some project 1"
    questions:
    - !!python/object/apply:collections.OrderedDict
      - - - pk
        - - slug
        - - object_id
        - - name
        - - input
  - project: 2
    pk: 2
    slug: "slugproject2"
    object_id: 11
    name: "some project 2"
    questions:
    - !!python/object/apply:collections.OrderedDict
      - - - pk
        - - slug
        - - object_id
        - - name
        - - input
  - project: 3
    pk: 3
    slug: "slugproject3"
    object_id: 12
    name: "some project 3"
    questions:
    - !!python/object/apply:collections.OrderedDict
      - - - pk
        - - slug
        - - object_id
        - - name
        - - input
  </code></pre>
  
  <p>As you can see the above is not usable for import or even readable export...</p>
  
  <p>Can you guys point me in the right direction on how to achieve a nested models dumpdata yaml export in django?</p>
  
  <p>Thanks!</p>