Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django shell: Command to load test fixture data?

Tags:

shell

django

Is there an easy way to load fixture data that I usually use in automated test runs in the interactive Django shell?

It might be awkward to have a mixture of model data that come from the database and others that come from a fixture. In my case, I have some read-only tables and wand to experiment with some data that I can discard afterwards.

I can probably load the fixture files like described here, but that's a bit cumbersome for repeated use...

like image 699
Sven Avatar asked Dec 17 '12 15:12

Sven


People also ask

What does the Django command manage py shell do?

When you run python manage.py shell you run a python (or IPython) interpreter but inside it load all your Django project configurations so you can execute commands against the database or any other resources that are available in your Django project.

How do I use fixtures in Django?

Providing data with fixtures The most straightforward way of creating a fixture if you've already got some data is to use the manage.py dumpdata command. Or, you can write fixtures by hand; fixtures can be written as JSON, XML or YAML (with PyYAML installed) documents.


2 Answers

I expect ./manage.py loaddata fixture_name.json is what you want.

like image 89
Daniel Roseman Avatar answered Sep 17 '22 20:09

Daniel Roseman


ilardm's answer points in the right direction, specifically what you want is:

from django.core.management import call_command
call_command('loaddata', 'fixture_name.json')

Edit: But the correct way to include fixtures in test cases is like this:

class TestThis(TestCase):
    fixtures = ['myfixture.json']

    def setUp(self):
        # Ready to test
like image 33
oldsea Avatar answered Sep 17 '22 20:09

oldsea