Datamigration in Django
Create empty migration:
python manage.py makemigrations --empty main
Where main
is your app name.
Add operation in generated code. For example, let's change Tip
model from main
app:
from django.db import migrations
from django.utils.text import slugify
def slugify_title(apps, schema_editor):
Tip = apps.get_model("main", "Tip")
for t in Tip.objects.all():
t.slug = slugify(t.title)
t.save()
class Migration(migrations.Migration):
dependencies = [
('main', '0008_tip_slug'),
]
operations = [
migrations.RunPython(slugify_title),
]
An important thing here that the signals and overridden save method of models that you use here will not be called. If you need it to be called, please create django manage.py command instead.