slugify service
This commit is contained in:
parent
483f076d1a
commit
273e417da2
8 changed files with 79 additions and 15 deletions
44
hub/services/migrations/0006_service_slug.py
Normal file
44
hub/services/migrations/0006_service_slug.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
# Generated by Django 5.1.5 on 2025-01-27 15:57
|
||||
|
||||
from django.db import migrations, models
|
||||
from django.utils.text import slugify
|
||||
|
||||
|
||||
def generate_service_slugs(apps, schema_editor):
|
||||
Service = apps.get_model("services", "Service")
|
||||
|
||||
for service in Service.objects.all():
|
||||
base_slug = f"{service.name}-{service.cloud_provider.name}"
|
||||
slug = slugify(base_slug)
|
||||
|
||||
counter = 1
|
||||
while Service.objects.filter(slug=slug).exists():
|
||||
slug = f"{slugify(base_slug)}-{counter}"
|
||||
counter += 1
|
||||
|
||||
service.slug = slug
|
||||
service.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("services", "0005_lead"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="service",
|
||||
name="slug",
|
||||
field=models.SlugField(max_length=250, blank=True),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.RunPython(
|
||||
generate_service_slugs, reverse_code=migrations.RunPython.noop
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="service",
|
||||
name="slug",
|
||||
field=models.SlugField(max_length=250, unique=True),
|
||||
),
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue