5 回答

TA貢獻1770條經驗 獲得超3個贊
您可以使用pycountrypackage很容易地自己生成國家/地區。
由于創建code的模型字段Country的最大長度為兩個字符,因此您需要使用該alpha_2代碼。
我通常對這類事情使用自定義管理命令。也許添加一個檢查以查看是否已經創建了任何對象,然后根據需要進行處理。
從外殼使用python manage.py create_countries
from address.models import Country
from pycountry import countries
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = 'Initialize Country model'
def handle(self, *args, **kwargs):
create_countries = [
Country(name=country.name[:40], code=country.alpha_2)
for country in countries
]
Country.objects.bulk_create(create_countries)
self.stdout.write(f'Created {len(countries)} countries.\n')
如果生產服務器沒有運行 Python/Django,那么您可以使用pycountry創建包含相關數據的 CSV 文件。假設您使用的是 PostgreSQL,那么您可以使用該COPY FROM命令來填充數據庫。
import csv
from pycountry import countries
with open('countries.csv', mode='w') as countries_file:
# specify delimiter because some countries have a comma
writer = csv.writer(countries_file, delimiter='\t')
writer.writerow(['id', 'name', 'code'])
writer.writerows([
[index + 1, country.name, country.alpha_2]
for index, country in enumerate(countries)
])

TA貢獻1788條經驗 獲得超4個贊
我建議您編寫一個簡單的管理命令,將pycountry中的數據導入您的地址模型(從此處借用的方法)。pycountry 是 ISO 標準國家/地區列表的包裝器 - 即,它與您將獲得的國家/地區列表一樣規范。
將所有國家/地區填充到您的模型中的管理命令Country如下所示:
import pycountry
from django.core.management.base import BaseCommand, CommandError
from address.models import Country
class Command(BaseCommand):
help = "Populates address.Country with data from pycountry."
def handle(self, *args, **options):
countries = [
Country(
code=country.alpha_2,
name=country.name[:40], # NOTE - concat to 40 chars because of limit on the model field
)
for country in pycountry.countries
]
Country.objects.bulk_create(countries)
self.stdout.write("Successfully added %s countries." % len(countries))
這將使用 ISO 國家/地區列表填充您的模型。
這里需要注意的是,該address.Country.name字段限制為 40 個字符(這對我來說似乎是一個有問題的設計決定,以及不使國家代碼唯一的決定 - ISO 2 字母代碼肯定是唯一的),所以上面的腳本截斷了適合的名字。如果這對您來說是個問題,我建議您建立自己的地址模型,借用django-address并提高字符限制。

TA貢獻1852條經驗 獲得超7個贊

TA貢獻1789條經驗 獲得超8個贊
如上所述,您需要自己添加實際數據。您需要準備它并進行一次性上傳。如果您只查找國家/地區數據,這是一個很好的來源。還有一個名為django-countries的 django 應用程序,它可以讓您擁有更多數據和控件,包括標志、ISO 代碼等。另一個具有 3 個字母代碼的數據庫是IBAN 列表。希望有幫助。

TA貢獻1853條經驗 獲得超18個贊
簡單更好。通過 django 文檔,您可以創建數據遷移:https ://docs.djangoproject.com/en/3.0/topics/migrations/#data-migrations
假設您的應用名稱models.py
是coops
,請執行以下操作:
第一的:
pip install django-countries
(我更喜歡 pipenv 安裝)https://github.com/SmileyChris/django-countries
第二:
添加
django_countries
到INSTALLED_APPS
第三:
制作一個空的遷移文件:
python manage.py makemigrations --empty coops
向前:
編輯遷移文件,例如:
vi coops/migrations/0002_auto_20200205_0421.py
文件內容:
# Generated by Django 2.2 on 2020-02-05 04:21
from django.db import migrations
def init_countries(apps, schema_editor):
from django_countries import countries
from address.models import Country
countries = [
Country(code=code, name=name) for code, name in countries
]
Country.objects.bulk_create(countries)
class Migration(migrations.Migration):
dependencies = [
('coops', '0001_initial'),
]
operations = [
migrations.RunPython(init_countries),
]
第五:
跑python manage.py migrate
遷移后 address_country 表應該有如下數據:
In [1]: from address.models import *
In [2]: Country.objects.all()
Out[2]: <QuerySet [<Country: Afghanistan>, <Country: Albania>, <Country: Algeria>, <Country: American Samoa>, <Country: Andorra>, <Country: Angola>, <Country: Anguilla>, <Country: Antarctica>, <Country: Antigua and Barbuda>, <Country: Argentina>, <Country: Armenia>, <Country: Aruba>, <Country: Australia>, <Country: Austria>, <Country: Azerbaijan>, <Country: Bahamas>, <Country: Bahrain>, <Country: Bangladesh>, <Country: Barbados>, <Country: Belarus>, '...(remaining elements truncated)...']>
In [3]: Country.objects.all().count()
Out[3]: 249
添加回答
舉報