Skip to content

Instantly share code, notes, and snippets.

@kkirsanov
Created April 27, 2017 09:21
Show Gist options
  • Save kkirsanov/b38a603db637c1c0adf6a92e9bda69d7 to your computer and use it in GitHub Desktop.
Save kkirsanov/b38a603db637c1c0adf6a92e9bda69d7 to your computer and use it in GitHub Desktop.
data migrations for django
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-04-04 08:08
from __future__ import unicode_literals
from django.db import migrations
# python manage.py dumpdata > dump.json
def load_fixture(apps, schema_editor):
from django.core.serializers import base, python
from django.core.management import call_command
# Save the old _get_model() function
old_get_model = python._get_model
# Define new _get_model() function here, which utilizes the apps argument to
# get the historical version of a model. This piece of code is directly stolen
# from django.core.serializers.python._get_model, unchanged.
def _get_model(model_identifier):
try:
return apps.get_model(model_identifier)
except (LookupError, TypeError):
raise base.DeserializationError("Invalid model identifier: '%s'" % model_identifier)
# Replace the _get_model() function on the module, so loaddata can utilize it.
python._get_model = _get_model
# Call loaddata command
call_command('loaddata', 'dump.json', app_label='pay')
# Restore old _get_model() function
python._get_model = old_get_model
class Migration(migrations.Migration):
dependencies = [
('lpmk', '0010_auto_20170407_1033'),
('sessions', '0001_initial'),
]
operations = [
migrations.RunPython(load_fixture)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment