如何将姜戈国家整合到现有的模式



我在生产数据库中有一个现有的国家类,但想使用Django_Countries模型来更新我使用的所有国家。

这是现有的模型。它允许用户创建一个国家并上传他们国家的国旗

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

我想删除用户创建国家的选项,只选择一个国家。

我真的不能改变现有的模型,因为它有很多依赖关系。我只是想添加名称和标志到现有的模型

最后只是将国家转储到json文件中,并编写迁移以更新数据库。

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),
]

相关内容

最新更新