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...
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.
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.
I expect ./manage.py loaddata fixture_name.json
is what you want.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With