djangopyyamldumpdata

how to Django dumpdata nested models in yaml


So I have this awesome Django app which gives me satisfaction and such. But now the problem, I want to use dumpdata (or something that does the same) to export a model with a nested other model in yaml format.

Lets say I have two models, Project and Questions. Each Project can have it's owns set of Questions.

The code looks something like this:

Project Model:

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)

Questions Model:

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()

The Project model has his own app and so has the Questions. The structure looks like this:

- Django
  - Apps
    - Project
    - Questions

Whenever I want to export my database I'll do the following:

./manage.py dumpdata --indent 4 --format yaml > dbdump.yaml

Although this works, and I can later import it with LoadData, 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:

Projects section:

-   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"]}

Questions section:

-   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"}

What I really want is to export the yaml file like so:

-   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"
            }            
        }
    }

To achieve this I implemented a custom serialiser inside projects:

- Django
  - Apps
    - Project
      - Management
        - Commands
          - test.py
    - Questions

The code looks like:

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)

I can run the code by running:

./manage.py test

Only this exports my models like so:

- 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

As you can see the above is not usable for import or even readable export...

Can you guys point me in the right direction on how to achieve a nested models dumpdata yaml export in django?

Thanks!


Solution

  • So I figured out how to handle the export/import.

    To export to a proper yaml file format I did the following:

    ...
    class Command(BaseCommand):
    
    def handle(self, *args, **options):
        with open('result.yaml', 'w') as yaml_file:
            model = serializer.Meta.model
            json_object = []
            json_data = json.dumps(serializer(model_data).data)
            json_object.append(json.loads(json_data))
    
            yaml.dump(
                json_object,
                yaml_file,
                default_flow_style=False,
                allow_unicode=False,
                encoding=None
            )
    

    This export to a proper yaml file.

    Then to import the yaml file I did the following:

    ...
    class Command(BaseCommand):
        def handle(self, *args, **options):
            with open('result.yaml', 'r') as yaml_file:
                yaml_list = yaml.load(yaml_file.read())
                for data in yaml_list:
                    ...process file
    

    And that's it!