Advertisement
Guest User

Untitled

a guest
May 29th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. ###Dump fixture data to be loaded###
  2. ```python manage.py dumpdata --format=json your_app > /app/path/your_app/fixtures/fixture_data_file.json```
  3.  
  4. ###Create an empty migration file###
  5. ```python manage.py makemigrations --empty your_app```
  6.  
  7. ###Edit the newly created migration file for your_app###
  8.  
  9. ```
  10. # -*- coding: utf-8 -*-
  11. from __future__ import unicode_literals
  12.  
  13. from django.db import models, migrations
  14. from django.core.management import call_command
  15.  
  16. fixture = 'fixture_data_file'
  17.  
  18.  
  19. def load_fixture_data(apps, schema_editor):
  20. """load in the 'your_fixture_data_file' to the database"""
  21. call_command('loaddata', fixture, app_label='your_app')
  22.  
  23.  
  24. def unload_fixture_data(apps, schema_editor):
  25. """Brutally deleting all entries for this model"""
  26.  
  27. YourModel = apps.get_model("your_app", "YourModel")
  28. YourModel.objects.all().delete()
  29.  
  30.  
  31. class Migration(migrations.Migration):
  32.  
  33. dependencies = [
  34. ('your_app', '0002_auto_20150418_1326'),
  35. ]
  36.  
  37. operations = [
  38. migrations.RunPython(load_fixture_data, reverse_code=unload_fixture_data),
  39. ]
  40. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement