9
you can do it by manual edit migration,
- do makemigrations with null
- do makemigrations with not null
- Edit first make migration by add datamigration with update and move operations from second migrate file
- remove the second migrations file,
for example:
from django.db import migrations, models
from django.db.models import F
def set_price_total(apps, schema_editor):
# Change the myapp on your
Model = apps.get_model('myapp', 'Model')
Model.objects.update(price_total=F('price'))
class Migration(migrations.Migration):
dependencies = [
('myapp', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='model',
name='price_total',
field=models.DecimalField(
decimal_places=2, max_digits=10, null=True),
),
migrations.RunPython(set_price_total),
migrations.AlterField(
model_name='model',
name='price_total',
field=models.DecimalField(
decimal_places=2, default=1, max_digits=10),
preserve_default=False,
),
]
3
You are on track to do it properly.
Just make sure step 3 in done in a datamigration
(certainly not through django shell).
This way you wonβt forget to run it on production.
Iβm pretty sure you canβt do both add the column and setting the value to be the same as another column.
To convince yourself, you can search for vanilla SQL implementation like https://stackoverflow.com/a/13250005/1435156
- [Django]-How Can i integrate c++ and Python with SWIG
- [Django]-Why Django translation does not use the base language in javascript?
Source:stackexchange.com