-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Make unique school names migration
- Loading branch information
1 parent
21524fb
commit bb5b2b2
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
] |