Add migration to repopulate slug field based on library key

This commit is contained in:
Kenneth Reitz
2024-03-25 12:36:15 -04:00
parent 3273d1179b
commit 833503ecff
2 changed files with 23 additions and 1 deletions

View File

@@ -0,0 +1,21 @@
from django.db import migrations
from django.utils.text import slugify
from libraries.models import Library
def migrate_to_key_slug(apps, schema_editor):
"""Migrate the key to the slug field."""
for library in Library.objects.all():
library.slug = slugify(library.key)
library.save()
class Migration(migrations.Migration):
dependencies = [
("libraries", "0019_merge_20240218_0058"),
]
operations = [migrations.RunPython(migrate_to_key_slug)]

View File

@@ -139,7 +139,8 @@ class Library(models.Model):
"""
# Generate slug based on name
if not self.slug:
slug = slugify(self.name)
# Base the slug name off of the key from the gitmodules file.
slug = slugify(self.key)
# If there is a library with that slug, try a slug based on the key from the
# gitmodules file