Skip to content

Instantly share code, notes, and snippets.

@evilchili
Created December 8, 2014 15:31
Show Gist options
  • Save evilchili/7e05fd23784a9d53398c to your computer and use it in GitHub Desktop.
Save evilchili/7e05fd23784a9d53398c to your computer and use it in GitHub Desktop.
load django fixtures into memory
from django.core import serializers
from django.core.management.commands.loaddata import Command as LoadDataCommand
import os
def read_fixture(file_name):
"""
Read a fixture and return a list of objects without updating the database.
This method is derived from django.core.management.commands.Command.load_label(),
and we re-use a lot of the Command class to save some work.
Example:
I use this to read fixtures when doing data migrations, where I need to (re)generate
records safely, but do not want to preserve primary keys. To that end, I place this
method in app/migrations/__init__.py. Then, in my migration:
from . import read_fixture
class Migration(DataMigration):
def forwards(self, orm):
(fh, objects) = read_fixture('my_data.json')
for o in objects:
# ...do whatever...
# save the fixture in the db
o.save()
fh.close()
"""
l = LoadDataCommand()
# if file_name is absolute, do not try to resolve its relative location
if file_name[0] == '/':
fixture_path = file_name
# assume the fixture is in the 'fixtures' directory parallel to the migrations directory.
else:
fdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'fixtures')
fixture_path = os.path.abspath(os.path.join(fdir, file_name))
# set up the serializer and compression formats. we only support JSON, but if you want
# other formats define theirr handlers here (this is copied from the loaddata method).
l.serialization_formats = serializers.get_public_serializer_formats()
l.compression_formats = {
None: open,
#'gz': gzip.GzipFile,
#'zip': SingleZipReader
}
# figure out the serialization and compression method used
_, ser_fmt, cmp_fmt = l.parse_name(file_name)
# open the fixture file
open_method = l.compression_formats[cmp_fmt]
fixture = open_method(fixture_path, 'r')
# try to generate objects from the fixture
try:
objects = serializers.deserialize(ser_fmt, fixture, ignorenonexistent=True)
except Exception as e:
e.args = ("Problem reading fixture '%s': %s" % (file_name, e),)
fixture.close()
raise
# return the open filehandle and object generator
return (fixture, objects)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment