Rerun a Django data migration

Fake back to the migration before the one you want to rerun. ./manage.py migrate –fake yourapp 0010_my_previous_data_migration Then rerun the migration. ./manage.py migrate yourapp 0011_my_data_migration Then you can fake back to the most recent migration that you have run. In your case, you said that 0011 was the latest, so you can skip this stage. … Read more

Django migrations RunPython not able to call model methods

Model methods are not available in migrations, including data migrations. However there is workaround, which should be quite similar to calling model methods. You can define functions inside migrations that mimic those model methods you want to use. If you had this method: class Order(models.Model): ”’ order model def goes here ”’ def get_foo_as_bar(self): new_attr=”bar: … Read more

Django 1.8: Create initial migrations for existing schema

Finally got it to work, although I don’t know why and I hope it will work in the future. After doing numerous trials and going through Django’s dev site (link). Here are the steps (for whoever runs into this problem): Empty the django_migrations table: delete from django_migrations; For every app, delete its migrations folder: rm … Read more

Django migration with uuid field generates duplicated values

Here is an example doing everything in one single migration thanks to a RunPython call. # -*- coding: utf-8 -* from __future__ import unicode_literals from django.db import migrations, models import uuid def create_uuid(apps, schema_editor): Device = apps.get_model(‘device_app’, ‘Device’) for device in Device.objects.all(): device.uuid = uuid.uuid4() device.save() class Migration(migrations.Migration): dependencies = [ (‘device_app’, ‘XXXX’), ] operations … Read more

How to reset migrations in Django 1.7

I would just do the following on both the environments (as long as the code is the same) Delete your migrations folder DELETE FROM django_migrations WHERE app = <your app name> . You could alternatively just truncate this table. python manage.py makemigrations python manage.py migrate –fake After this all your changes should get detected across … Read more

Django Migrations Add Field with Default as Function of Model

I just learned how to do this with a single migration! When running makemigrations django should ask you to set a one-off default. Define whatever you can here to keep it happy, and you’ll end up with the migration AddField you mentioned. migrations.AddField( model_name=”series”, name=”updated_as”, field=models.DateTimeField(default=????, auto_now=True), preserve_default=False, ), Change this one operation into 3 … Read more