[Answered ]-Is there an easy way to create a fixture in Django without using dump_data?

2๐Ÿ‘

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")
๐Ÿ‘คdjq

0๐Ÿ‘

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.

๐Ÿ‘คeugene

Leave a comment