I’m writing a migration to fix some broken links in one of the fields on my model, but the changes don’t save to the db:
from django.db import migrations
from django.db.models import Q
def fix_invalid_links(apps, schema_editor):
Question = apps.get_model('game', 'Question')
for question in Question.objects.filter(~Q(valid_links=[])):
links = question.valid_links
fixed_links = []
for link in links:
ext = '.' + link.rsplit('.', 1)[1]
match ext:
case '.j':
fixed_links.append(link + 'pg')
case '.jp':
fixed_links.append(link + 'g')
case '.w':
fixed_links.append(link + 'mv')
case '.wm':
fixed_links.append(link + 'v')
case '.m':
fixed_links.append(link + 'p3')
case '.mp':
fixed_links.append(link + '3')
case _:
fixed_links.append(link)
if set(links) != set(fixed_links):
print(f'links: {links}')
print(f'fixed_links: {fixed_links}')
question.valid_links = fixed_links
question.save()
class Migration(migrations.Migration):
dependencies = [
('game', '0008_question_valid_links'),
]
operations = [
migrations.RunPython(
fix_invalid_links, migrations.RunPython.noop
),
]
Judging by what’s being printed the logic seems sound, it’s just not saving the changes. I also tried it as a management command but had the same problem. Why isn’t this persisting?