Search code examples
djangopython-3.xiterationdata-migration

Django/Python: How to iterate through a list in a dictionary for migration/migrate data


I’m trying to set up migration files to load values into my tables. I am creating countries/states in my tables. I would like to set it up in a way that I can put each country in its own file, and then run through all the countries on migration. I successfully got all the names in separately, but I’m trying to make it easier.

UPDATE:

Thanks for help, I got it all to work the way I want it to. Here is my end result.

models:

class Country(models.Model):
    country_code = models.CharField(
        primary_key=True,
        max_length=3,
        verbose_name='Country Code',
        help_text='3 Letter Country Code',
        )
    country_name = models.CharField(
        "Country Name",
        max_length=30,
        unique=True,
        )

class State(models.Model):
    key = models.AutoField(
        primary_key=True,
        )
    country = models.ForeignKey(
        'Country',
        on_delete=models.CASCADE,
        verbose_name='Country',
        help_text='State located in:'
        )
    state_name = models.CharField(
        "State or Province",
        max_length=100,
        )
    state_code = models.CharField(
        "State Code",
        max_length=30,
        help_text='Mailing abbreviation'
        )

Migration Data files:

"""
Canada migration information.
    Stored in dictionary = canada

copy into migration_data_migrate_countries
    from .migration_country_Canada import canada

Models:
    Country
    State
"""

# Country
import_country = ['CAN', 'CANADA',]

# State
import_states = [

    ['AB', 'ALBERTA'],
    ['BC', 'BRITISH COLUMBIA'],
    ['MB', 'MANITOBA'],
    etc...

]

# 'import' into migration file
canada = {

    'country': import_country,
    'states': import_states,

}

Second country file:

# Country
import_country = ['USA', 'UNITED STATES',]

# State
import_states = [

    ['AL', 'ALABAMA'],
    ['AK', 'ALASKA'],
    ['AZ', 'ARIZONA'],
    etc...

]

# 'import' into migration file
united_states = {

    'country': import_country,
    'states': import_states,

    }

import method:

# Keep imports alphabetized by country name.
from .migration_country_Canada import canada
from .migration_country_UnitedStates import united_states

list_of_countries = [

    canada,
    united_states,

]


def migrate_countries(apps, schema_editor):
    Country = apps.get_model('app', 'Country')
    State = apps.get_model('app', 'State')

    for country in list_of_countries:
        import_country = country['country']
        states = country['states']

        current_country = Country.objects.get_or_create(
                                country_code=import_country[0],
                                country_name=import_country[1]
                                )

        # False = already exists. True = created object.
        print(import_country, current_country)

        for s in states:
            state_country = Country.objects.get(
                                country_code=import_country[0])
            state = State.objects.get_or_create(
                            country=state_country,
                            state_code=s[0],
                            state_name=s[1],
                            )

            # False = already exists. True = created object.
            print(s, state)

Then I run python3 manage.py makemigrations --empty app and edit the migration file:

from django.db import migrations

from app.migration_countries.migration_data_migrate_countries import *


class Migration(migrations.Migration):

    dependencies = [
        ('app', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(migrate_countries),
    ]

Then run python3 manage.py migrate and get results in the terminal

...               # Added Canada first which is why YUKON is False
['YT', 'YUKON'] (<State: State object (75)>, False)
['USA', 'UNITED STATES'] (<Country: Country object (USA)>, True)
['AL', 'ALABAMA'] (<State: State object (76)>, True)
...

And when I want to add another country, france, I make a france file and change my list to:

list_of_countries = [

    canada,
    france,
    united_states,

]

And any changes I make should stay up to date. Hopefully. Any suggestions, feel free to let me know.


Solution

  • I guess the problem the way you initially are trying to approach this task. I think you should update your dictionary:

    canada = {
        'country': import_country,
        'states': import_states,
    }
    

    Keep in mind that a key should be an immutable object.

    for country in list_of_countries:
        import_country = country['country']
        states = country['states']
    
        current_country = Country.objects.get_or_create(
                                    country_code=c[0],
                                    country_name=c[1]
                                    )
        current_country.states = states
    

    I am not sure what you are trying to achieve, but if you provide a better description I can update my answer.