I want to add a new column to an already existing table, but I want to give it a default value dependent on already existing data:
e.g. Each record has a start_date
. Now I want to add an open_until
column, and I want to fill it with the value for start_date
of each existing record. (the upcoming records will be able to pick different value)
Is there a friendly way to do this?
You can also do it within South. The only caveat is that you need two steps for that:
A schema migration that adds the open_until column
from django.db import models
import datetime
class MyModel(models.Model):
start_date = models.DateField(),
open_until = models.DateField(default=datetime.date.today),
$ python manage.py schemamigration --auto appname
A data migration that fills existing rows with the value of that other column
$ python manage.py datamigration appname populate_open_until
import datetime
class Migration(DataMigration):
def forwards(self, orm):
"Set open_until value to that of start_date for existing rows"
for t in orm.MyModel.objects.all():
t.open_until = t.start_date
t.save()
def backwards(self, orm):
"Revert back to default"
for t in orm.MyModel.objects.all():
t.open_until = datetime.date.today
t.save()
(optional) In step 1 you can either provide a temporary default value or make it optional and add a 3rd step
In Python 3.8 I first add fields to MyApp
models file and it looks like:
from django.db import models
import datetime
class MyModel(models.Model):
start_date = models.DateField(),
open_until = models.DateField(default=datetime.date.today),
Then, after running manage.py makemigrations
add this lines to new migrations file is created:
def forward(apps, schema_editor):
my_model_objects = apps.get_model('MyApp', 'MyModel').objects.all()
for t in my_model_objects:
t.open_until = t.start_date
t.save()
def reverse(apps, schema_editor):
pass
class Migration(migrations.Migration):
operations = [
" the base operations is here " ,
migrations.RunPython(forward, reverse),
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With