nilanjan
nilanjan

Reputation: 679

Is there an easy way to create a fixture in Django without using dump_data?

Is there an easy way to create fixtures, without using dump_data? We have a complex database and it seems like creating the fixtures directly, without using dump_data would mean a lot of work.

Can I create objects and write them to json so they can be used as fixtures?

Note that we are using multiple applications and data is referenced between applications.

Upvotes: 1

Views: 528

Answers (2)

djq
djq

Reputation: 15308

I've used Django Dynamic Fixtures for several years and found it really great. It generates fixtures based on your model definitions.

If you have a model Project you can generate your fixtures in a test environment using the command G(Project) and optionally customize it with G(Project, name='test') etc.

from django_dynamic_fixture import G
from apps.projects.models import Project

class TestProject(TestCase):
    """
    Test project name
    """
    def setUp(self):
        self.project1 = G(Project)
        self.project2 = G(Project, name="my project")

    def test_project(self):
        self.assertTrue(self.project1)

    def test_name(self):
        self.assertEqual(self.project2.name, "my project")

Upvotes: 2

eugene
eugene

Reputation: 41785

How about using serializers of DRF? (or any library that you are familiar with)

You can serialize objects to json easily using DRF.

Just output them to file.

Upvotes: 0

Related Questions