如何将带有外键的 .csv table 导入 Django 数据库
How to import a .csv table with a foreign key into a django database
这是我的第一个 Django 项目,我很难将数据从 .csv 文件加载到具有外键的模型中。
这是我的模型:
class Artist(models.Model):
artistID = models.IntegerField(primary_key=True, null=False, unique=True)
artistName = models.CharField(max_length=50)
artistNotes = models.TextField(blank=True)
class Art(models.Model):
artID = models.IntegerField(primary_key=True, null=False, unique=True)
artistID = models.ForeignKey(Artist, db_column='artistID', on_delete=models.CASCADE, default = 1)
title = models.CharField(max_length=100, default = "No Title Given")
location = models.CharField(max_length=100)
owner = models.CharField(max_length=50, blank=True)
origin = models.CharField(max_length=150, blank=True)
medium = models.CharField(max_length=50, blank=True)
artNotes = models.TextField(blank=True)
我写了一个可以导入数据的视图:
def importArt(request):
myFile = open('misc/Art.csv', 'r')
for line in myFile:
line = line.split(',')
temp = Art.objects.create()
temp.artID = line[0]
temp.artistID = line[1]
temp.title = line[2]
temp.location = line[3]
temp.owner = line[4]
temp.origin = line[5]
temp.medium = line[6]
temp.artNotes = line[7]
temp.save()
myFile.close()
return render(request, 'dtccArt/importArt.html', {})
此策略对艺术家 table 很有效,但这是我收到的错误:无法分配“'2'”:"Art.artistID" 必须是 "Artist" 实例。
我的第一行数据是这样的:
1,2,Wisdom & Knowledge,Main Library,College,Visiting Artist at DTCC 19??-19??,Stone Sculpture,,
在到达这个卡点之前我修正了两个错误。我在 Art 模型的 ArtistID 字段中添加了 db_column='artistID' 和 default = 1 。默认值 = 1 指的是未知艺术家,以防某件艺术品的艺术家未知。
有人可以解释错误消息的含义、一些关于如何修复它的提示,或者更简单的方法来将 .csv 数据导入现有的 Django 模型吗?
提前致谢!
安德里亚
改变
temp.artistID = line[1]
至
temp.artistID = Artist.objects.get(int(line[1]))
在 Rakesh 的帮助下,我找到了解决方法。以下视图有效:
def importArt(request):
myFile = open('misc/Art.csv', 'r')
for line in myFile:
line = line.split(',')
temp = Art.objects.create()
temp.artID = line[0]
if line[1] != '':
temp.artistID = Artist.objects.get(pk = (line[1]))
else:
temp.artistID = Artist.objects.get(pk = 1)
if line[2] != '':
temp.title = line[2]
else:
temp.title = "Unknown"
temp.location = line[3]
temp.owner = line[4]
temp.origin = line[5]
temp.medium = line[6]
temp.artNotes = line[7]
temp.save()
myFile.close()
return render(request, 'dtccArt/importArt.html', {})
ArtistID 和 Title 是必填字段,因此我在 "Unknown" 中对缺少的 Title 进行了硬编码。 PK = 1 位艺术家的名称为未知。
我使用 Rakesh 和 ajwong4 anwsers 想出了这个通用解决方案 pandas
Rakesh 和 ajwong4 非常感谢!
这里是通用的解决代码
#First define your Django Enviromental variables
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DjangoProjectName.settings")
import django
django.setup()
import pandas as pd
import numpy as np
#Import Required Django model
from djangoApp.models import * #models
# Import CSV file
df = pd.read_csv('csv_url.csv')
# Do required pre-processing on panda dataframe
# such as data cleaning, data format settings etc..
# Iterater throught panda dataframe and save data in Django model
for index, row in df.iterrows():
# create django model
samplemodelObject = SampleModel()
# Normal Fields ( Non-foreign key fields) adding
samplemodelObject.field_name01 = row['Field_01']
# Adding Foreign Key Field
samplemodelObject.field_foreignkey = ForeignKeyModel.objects.get( fk_key = row['fk_value_field']
# save data model instance
samplemodelObject.save()
samplemodelObject.clear()
这是我的第一个 Django 项目,我很难将数据从 .csv 文件加载到具有外键的模型中。
这是我的模型:
class Artist(models.Model):
artistID = models.IntegerField(primary_key=True, null=False, unique=True)
artistName = models.CharField(max_length=50)
artistNotes = models.TextField(blank=True)
class Art(models.Model):
artID = models.IntegerField(primary_key=True, null=False, unique=True)
artistID = models.ForeignKey(Artist, db_column='artistID', on_delete=models.CASCADE, default = 1)
title = models.CharField(max_length=100, default = "No Title Given")
location = models.CharField(max_length=100)
owner = models.CharField(max_length=50, blank=True)
origin = models.CharField(max_length=150, blank=True)
medium = models.CharField(max_length=50, blank=True)
artNotes = models.TextField(blank=True)
我写了一个可以导入数据的视图:
def importArt(request):
myFile = open('misc/Art.csv', 'r')
for line in myFile:
line = line.split(',')
temp = Art.objects.create()
temp.artID = line[0]
temp.artistID = line[1]
temp.title = line[2]
temp.location = line[3]
temp.owner = line[4]
temp.origin = line[5]
temp.medium = line[6]
temp.artNotes = line[7]
temp.save()
myFile.close()
return render(request, 'dtccArt/importArt.html', {})
此策略对艺术家 table 很有效,但这是我收到的错误:无法分配“'2'”:"Art.artistID" 必须是 "Artist" 实例。
我的第一行数据是这样的:
1,2,Wisdom & Knowledge,Main Library,College,Visiting Artist at DTCC 19??-19??,Stone Sculpture,,
在到达这个卡点之前我修正了两个错误。我在 Art 模型的 ArtistID 字段中添加了 db_column='artistID' 和 default = 1 。默认值 = 1 指的是未知艺术家,以防某件艺术品的艺术家未知。
有人可以解释错误消息的含义、一些关于如何修复它的提示,或者更简单的方法来将 .csv 数据导入现有的 Django 模型吗?
提前致谢! 安德里亚
改变
temp.artistID = line[1]
至
temp.artistID = Artist.objects.get(int(line[1]))
在 Rakesh 的帮助下,我找到了解决方法。以下视图有效:
def importArt(request):
myFile = open('misc/Art.csv', 'r')
for line in myFile:
line = line.split(',')
temp = Art.objects.create()
temp.artID = line[0]
if line[1] != '':
temp.artistID = Artist.objects.get(pk = (line[1]))
else:
temp.artistID = Artist.objects.get(pk = 1)
if line[2] != '':
temp.title = line[2]
else:
temp.title = "Unknown"
temp.location = line[3]
temp.owner = line[4]
temp.origin = line[5]
temp.medium = line[6]
temp.artNotes = line[7]
temp.save()
myFile.close()
return render(request, 'dtccArt/importArt.html', {})
ArtistID 和 Title 是必填字段,因此我在 "Unknown" 中对缺少的 Title 进行了硬编码。 PK = 1 位艺术家的名称为未知。
我使用 Rakesh 和 ajwong4 anwsers 想出了这个通用解决方案 pandas
Rakesh 和 ajwong4 非常感谢!
这里是通用的解决代码
#First define your Django Enviromental variables
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DjangoProjectName.settings")
import django
django.setup()
import pandas as pd
import numpy as np
#Import Required Django model
from djangoApp.models import * #models
# Import CSV file
df = pd.read_csv('csv_url.csv')
# Do required pre-processing on panda dataframe
# such as data cleaning, data format settings etc..
# Iterater throught panda dataframe and save data in Django model
for index, row in df.iterrows():
# create django model
samplemodelObject = SampleModel()
# Normal Fields ( Non-foreign key fields) adding
samplemodelObject.field_name01 = row['Field_01']
# Adding Foreign Key Field
samplemodelObject.field_foreignkey = ForeignKeyModel.objects.get( fk_key = row['fk_value_field']
# save data model instance
samplemodelObject.save()
samplemodelObject.clear()