# Generated by Django 5.1.5 on 2025-01-29 17:08 import django.db.models.deletion from django.db import migrations, models from games.models import Game def copy_platform_to_game(apps, schema_editor): single_edition_games = Game.objects.annotate( num_editions=models.Count("editions") ).filter(num_editions=1) multi_edition_games = Game.objects.annotate( num_editions=models.Count("editions") ).filter(num_editions__gt=1) for game in single_edition_games: game.platform = game.editions.first().platform game.save() for game in multi_edition_games: all_editions = game.editions.all() for e in all_editions: # game with this platform edition already exists if game.platform == e.platform: print( f"Game '{game}' with ID '{game.pk}' already has edition with platform '{game.platform}', skipping creation." ) else: print( f"Game '{game}' with ID '{game.pk}' missing edition with platform '{e.platform}', creating..." ) Game.objects.create( name=e.name, sort_name=e.sort_name, platform=e.platform, year_released=e.year_released, ) class Migration(migrations.Migration): dependencies = [ ("games", "0047_alter_edition_game"), ] operations = [ migrations.AddField( model_name="game", name="platform", field=models.ForeignKey( blank=True, default=None, null=True, on_delete=django.db.models.deletion.SET_DEFAULT, to="games.platform", ), ), migrations.RunPython(copy_platform_to_game), ]