How to integrate django countries in to an existing model

386 Views Asked by At

I have an existing countries class in a database in production but would like to use the Django_Countries model to update all the countries I use.

Here is the existing model. It allows users create a country and upload a flag of their country

class Country(models.Model):
    name = models.CharField(_("Name"))
    icon = models.ImageField(_("Icon"),upload_to=os.path.join('images', 'flags'))

I want to remove the option of a user creating a country and just have select a country.

I cant really change the existing model as there is a lot of dependencies on it. I just want to add the name and flag to the existing model.

1

There are 1 best solutions below

0
On

Finally ended up just doing a dump of the countries in to a json file and writing a migration to update the database.

def forwards(apps, schema_editor):
    Country = apps.get_model('country', 'Country')
    Region = apps.get_model('country', 'Region')

    Region.objects.filter(id=45).delete()

    with codecs.open(os.path.join(os.path.dirname(__file__), 'countries.json'), encoding='utf-8') as cfile:
    data = json.load(cfile)

    for country, regions in data.items():
        country, created = Country.objects.get_or_create(name=country)


class Migration(migrations.Migration):
    dependencies = [
    ('country', 'previous_migration'),
]

operations = [
    migrations.RunPython(forwards),
]