Data migration to populate `from_url_without_rest` field
@@ -5,6 +5,23 @@ from __future__ import unicode_literals
from django.db import migrations, models
+def forward(apps, schema_editor):
+ """Calculate new ``from_url_without_rest`` attribute."""
+ Redirect = apps.get_model('redirects', 'Redirect')
+
+ queryset = Redirect.objects.filter(
+ redirect_type='exact',
+ from_url__endswith='$rest',
+ )
+ for redirect in queryset:
+ redirect.from_url_without_rest = redirect.from_url.replace('$rest', '') or None
+ redirect.save()
+
+def backward(apps, schema_editor):
+ # just no-op
+ pass
+
+
class Migration(migrations.Migration):
dependencies = [
@@ -17,4 +34,5 @@ class Migration(migrations.Migration):
name='from_url_without_rest',
field=models.CharField(blank=True, db_index=True, help_text='Only for internal querying use', max_length=255, null=True),
),
+ migrations.RunPython(forward, backward),
]
