1๐
โ
I think it might be better to do this in four steps:
- first add a field
assignedtip_date
that containsNULL
for a start; - then run a data migration that populates the field with the truncated part of the
assignedtip
; - then remove the
assginedtip
field; and - finally rename the
assginedtip_date
field toassignedtip
.
The migration thus will look like:
# app_name/migrations/0123_some_name.py
from django.db import migrations, models
from django.db.models.functions import TruncDate
def migrate_date(apps, schema_editor):
MyModel = apps.get_model("app_name", "MyModel")
MyModel.objects.update(assignedtip_date=TruncDate('assignedtip'))
class Migration(migrations.Migration):
dependencies = [("migrations", "0001_initial")]
operations = [
migrations.AddField(
"MyModel", "assignedtip_date", models.DateField(null=True)
),
migrations.RunPython(migrate_date),
migrations.RemoveField("MyModel", "assignedtip"),
migrations.RenameField("MyModel", "assignedtip_date", "assignedtip"),
]
Source:stackexchange.com