簡體   English   中英

將數據從 CSV 加載到 Django 中的數據庫表

[英]Load data from CSV to Database tables in Django

我能夠成功加載國家和州,但在加載城市時出現錯誤

保存這個城市時出了點問題:0000000698 精確查找的 QuerySet 值必須限制為使用切片的一個結果。

這是我的 3 個模型“國家”、“州”和“城市”

class Country(BaseModel):
    id = models.CharField(
            primary_key=True,
            max_length=3,
            unique=True
        )

    class Meta:
        db_table = 'country'
class State(models.Model):
    id = models.CharField(
            primary_key=True,
            max_length=3,
            unique=True
        )
    country = models.ForeignKey(
            Country, 
            on_delete=models.CASCADE, 
            blank=True, 
            null=True
        )
    state_code =  models.CharField(
            max_length=4,
            null=True,
            blank=True,
            unique=True
        )
    capital_city =  models.CharField(
            max_length=10,
            null=True,
            blank=True,
        )

    class Meta:
        db_table = 'state'
class City(models.Model):
    id = models.CharField(
            primary_key=True,
            max_length=10,
            unique=True
        )

    country = models.ForeignKey(
            Country, 
            on_delete=models.CASCADE, 
            blank=True, 
            null=True
        )

    state = models.ForeignKey(
        State,
        on_delete=models.CASCADE,
        blank=True,
        null=True
    )


    class Meta:
        db_table = 'city'

這是我要上傳的示例 CSV 文件

國家.csv

10,Antarctica,Antarktika
16,American Samoa,Amerikanisch-Samoa
60,Bermuda,Bermuda
74,Bouvet Island,Bouvetinsel

狀態文件

1,276,BW,0000000111,Baden-Württemberg,Baden-Württemberg
2,276,BY,0000000165,Bavaria,Bayern
3,276,BE,0000000028,Berlin,Berlin
4,276,BB,0000000019,Brandenburg,Brandenburg
5,276,HB,0000000195,Bremen,Bremen
6,276,HH,0000000255,Hamburg,Hamburg

城市.csv

0000000001,276,BB,Bernau bei Berlin,Bernau bei Berlin
0000000002,276,BB,Blankenfelde-Mahlow,Blankenfelde-Mahlow
0000000003,276,BB,Brandenburg an der Havel,Brandenburg an der Havel
0000000004,276,BB,Cottbus,Cottbus
0000000005,276,BB,Eberswalde,Eberswalde
0000000029,276,BW,Aalen (Württemberg),Aalen (Württemberg)
0000000030,276,BW,Achern (Baden),Achern (Baden)
0000000031,276,BW,Albstadt (Württemberg),Albstadt (Württemberg)
0000000032,276,BW,Backnang,Backnang
0000000209,276,HE,Dillenburg,Dillenburg
0000000210,276,HE,Dreieich,Dreieich
0000000211,276,HE,Eschborn (Taunus),Eschborn (Taunus)
0000000212,276,HE,Flörsheim am Main,Flörsheim am Main

這是我導入“city.csv”的視圖/邏輯

def import_city_from_file(self):
        data_folder = os.path.join(settings.BASE_DIR, 'app_name', 'resources/city_csv')
        for data_file in os.listdir(data_folder):
            with open(os.path.join(data_folder, data_file), encoding='utf-8') as data_file:
                data = csv.reader(data_file)
                for data_object in data:
                    id = data_object[0]

                    if data_object[1] != '':
                        country = models.Country.objects.get(pk=(data_object[1]),)
                    else:
                        country= models.Country.objects.get(pk = 1)

                    if data_object[2] != '':
                        state = models.State.objects.filter(pk=(data_object[2]))
                        print(state)

                    else:
                        state= models.State.objects.filter(pk = 1)

                    long_description_eng = data_object[3]
                    long_description_deu = data_object[4]


                    try:
                        city, created = models.City.objects.get_or_create(
                                id=id,
                                country_id=country,
                                state_id = state,
                                long_description_eng=long_description_eng,
                                long_description_deu=long_description_deu,

                            )
                        if created:
                            city.save()
                            display_format = "\nCity, {}, has been saved."
                            print(display_format.format(city))
                    except Exception as ex:
                        print(str(ex))
                        msg = "\n\nSomething went wrong saving this city: {}\n{}".format(id, str(ex))
                        print(msg)

這是我嘗試為城市導入 CSV 文件時出現的錯誤在此處輸入圖片說明

對於解決此問題的任何幫助,我將不勝感激。

在您的城市get_or_create方法中,您使用了國家和州字段。 在這種情況下,您的國家和州變量必須是一個對象。 但是你的變量是查詢集( state = models.State.objects.filter(pk=(data_object[2]))這段代碼返回一個查詢集。)如果你確定這個狀態存在,你可以使用get()方法而不是filter()方法。)此外,如果您在城市get_or_create代碼參數中使用 state_id,請使用 city.id 而不是城市對象。因此,您必須更改兩個部分:

country= models.Country.objects.filter(pk = 1).first()

state = models.State.objects.filter(pk=data_object[2]).first()
''' this code return None if you have not any state object with this pk, 
    or you can use get() method but it raise DoesNotExist error, 
    you must     implement catching it 
'''

....


City.objects.get_or_create(
                            id=id,
                            country_id=country.id if country else None,
                            state_id = state.id if state else None,
                            long_description_eng=long_description_eng,
                            long_description_deu=long_description_deu,

                        )

我能夠通過將filter()更改為get()並使用 try 和 except 來捕獲錯誤來修復它。

def import_city_from_file(self):
        data_folder = os.path.join(settings.BASE_DIR, 'app_name', 'resources/city_csv')
        for data_file in os.listdir(data_folder):
            with open(os.path.join(data_folder, data_file), encoding='utf-8') as data_file:
                data = csv.reader(data_file)
                for data_object in data:
                    country = None
                    state = None
                    try:
                        country = models.Country.objects.get(id=data_object[0])
                    except:
                        pass
                    try:
                        state = models.State.objects.get(state_code=str(data_object[1]))

                    except:
                        pass
                    long_description_eng = data_object[2]
                    long_description_deu = data_object[3]


                    try:
                        city, created = models.City.objects.get_or_create(
                                id=id,
                                country_id=country,
                                state_id = state,
                                long_description_eng=long_description_eng,
                                long_description_deu=long_description_deu,

                            )
                        if created:
                            city.save()
                            display_format = "\nCity, {}, has been saved."
                            print(display_format.format(city))
                    except Exception as ex:
                        print(str(ex))
                        msg = "\n\nSomething went wrong saving this city: {}\n{}".format(id, str(ex))
                        print(msg)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM