Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import os
  4.  
  5. from django.db import models, migrations
  6. from django.core import serializers
  7.  
  8.  
  9.  
  10. fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__file__),
  11. '../fixtures'))
  12. fixture_filename = 'initial_data.json'
  13.  
  14.  
  15. def load_fixture(apps, schema_editor):
  16. fixture_file = os.path.join(fixture_dir, fixture_filename)
  17. with open(fixture_file, 'rb') as fixture:
  18. objects = serializers.deserialize('json', fixture,
  19. ignorenonexistent=True)
  20. for obj in objects:
  21. obj.save()
  22.  
  23.  
  24. def unload_fixture(apps, schema_editor):
  25. MyModel = apps.get_model("book", "Genre")
  26. MyModel.objects.all().delete()
  27.  
  28.  
  29. class Migration(migrations.Migration):
  30.  
  31. dependencies = [
  32. ('book', '0001_initial'),
  33. ]
  34.  
  35. operations = [
  36. migrations.RunPython(load_fixture, reverse_code=unload_fixture),
  37. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement