Skip to content

Commit

Permalink
fix: Make unique school names migration
Browse files Browse the repository at this point in the history
  • Loading branch information
faucomte97 committed Nov 16, 2023
1 parent 21524fb commit bb5b2b2
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cfl_common/common/migrations/0048_unique_school_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 3.2.20 on 2023-11-06 18:56

from django.apps.registry import Apps
from django.db import migrations, models


def unique_school_names(apps: Apps, *args):
School = apps.get_model("common", "School")

schools = School.objects.values("name").annotate(name_count=models.Count("name")).filter(name_count__gt=1)
for school in schools:
schools = list(School.objects.filter(name=school["name"]).order_by("id"))
for index in range(school["name_count"]):
if index > 0:
school = schools[index]
school.name += f" {index + 1}"
school.save()


class Migration(migrations.Migration):
dependencies = [
("common", "0047_delete_school_postcode"),
]

operations = [
migrations.RunPython(code=unique_school_names, reverse_code=migrations.RunPython.noop),
migrations.AlterField(
model_name="school",
name="name",
field=models.CharField(max_length=200, unique=True),
),
]

0 comments on commit bb5b2b2

Please sign in to comment.