Login

Django load global fixtures test helper

Author:
vaz
Posted:
July 25, 2011
Language:
Python
Version:
1.2
Score:
1 (after 1 ratings)

This lets you load global fixtures from a directory you set as FIXTURES_ROOT in your settings.py.

For example, setting FIXTURES_ROOT to /path/to/myproject/fixtures/

I tend to like to keep fixtures that should be loaded when a new instance of a site is deployed, but not auto-loaded (so they won't rewrite any data that comes after), in a project-level fixtures directory like this.

Sometimes these fixtures can be useful in your test suites, so this is a convenient way to load them.

Usage:

load_global_fixtures('sites.json', 'contacts.json') load_global_fixtures('sites.json', 'contacts.json', fixtures_root='/some/other/path', verbosity=1)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import os

from django.core import management
from django.conf import settings


def load_global_fixtures(*fixtures, **opts):
    options = dict(fixtures_root=settings.FIXTURES_ROOT, verbosity=0)
    options.update(opts)

    for fixture in fixtures:
        management.call_command('loaddata',
                os.path.join(options['fixtures_root'], fixture),
                verbosity=options['verbosity'])

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.