Created
April 17, 2015 10:22
-
-
Save tomviner/f0caaca579e72756aecf to your computer and use it in GitHub Desktop.
Load a django fixture, but error if the file can't be found
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from django.core import management | |
from django.conf import settings | |
def load_ensured_fixture(fixture_path): | |
""" | |
Load a fixture with a path like 'my_app/fixtures/test_initial_data.json' | |
But raise an exception if it's not found | |
""" | |
path_root = os.path.dirname(settings.DJANGO_ROOT) | |
# ensure the fixture is found on disk | |
abs_path = os.path.join(path_root, fixture_path) | |
assert os.path.exists(abs_path), "Fixture not found: {}".format(abs_path) | |
# load the actual fixture, django won't error if the file's not found, hence the check above | |
management.call_command('loaddata', abs_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment