import django.db.models.deletion from django.db import migrations, models def migrate_location_strings(apps, schema_editor): Plant = apps.get_model('plants', 'Plant') Location = apps.get_model('plants', 'Location') for plant in Plant.objects.all(): name = (plant.location_old or '').strip() if name: loc, _ = Location.objects.get_or_create(name=name) plant.location_new = loc plant.save(update_fields=['location_new']) class Migration(migrations.Migration): dependencies = [ ('plants', '0003_add_bloom_months'), ] operations = [ migrations.CreateModel( name='Location', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=200, unique=True)), ], options={ 'ordering': ['name'], }, ), migrations.CreateModel( name='PlantPhoto', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('image', models.ImageField(upload_to='plants/photos/')), ('is_thumbnail', models.BooleanField(default=False)), ('uploaded_at', models.DateTimeField(auto_now_add=True)), ('plant', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='photos', to='plants.plant')), ], options={ 'ordering': ['uploaded_at'], }, ), migrations.RenameField( model_name='plant', old_name='location', new_name='location_old', ), migrations.AddField( model_name='plant', name='location_new', field=models.ForeignKey( blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='plants', to='plants.location', ), ), migrations.RunPython(migrate_location_strings, migrations.RunPython.noop), migrations.RemoveField( model_name='plant', name='location_old', ), migrations.RenameField( model_name='plant', old_name='location_new', new_name='location', ), ]