Created
April 10, 2019 13:42
-
-
Save BenAtWide/3485cae5ee177532c4e5073898007f58 to your computer and use it in GitHub Desktop.
Django docs example of a basic data migration https://docs.djangoproject.com/en/1.11/topics/migrations/#data-migrations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import migrations | |
def combine_names(apps, schema_editor): | |
# We can't import the Person model directly as it may be a newer | |
# version than this migration expects. We use the historical version. | |
Person = apps.get_model('yourappname', 'Person') | |
for person in Person.objects.all(): | |
person.name = '%s %s' % (person.first_name, person.last_name) | |
person.save() | |
class Migration(migrations.Migration): | |
dependencies = [ | |
('yourappname', '0001_initial'), | |
] | |
operations = [ | |
migrations.RunPython(combine_names), | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment