这是我的第一个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', {})
此策略在Artist表上工作正常,但这是我遇到的错误:无法分配“'2'”:“ Art.artistID”必须是“ Artist”实例。
我的第一行数据如下:
1,2,Wisdom & Knowledge,Main Library,College,Visiting Artist at DTCC 19??-19??,Stone Sculpture,,
在解决此问题之前,我修复了两个错误。我在Art模型的ArtistID字段中添加了 db_column ='artistID'和默认= 1 。默认值= 1表示未知艺术家,以防某位艺术家对于某件艺术品不知道。
有人可以解释该错误消息的含义,一些有关如何修复该错误的提示,或者是一种将.csv数据导入现有Django模型的简便方法吗?
提前谢谢! 安德里亚
答案 0 :(得分:0)
更改
temp.artistID = line[1]
到
temp.artistID = Artist.objects.get(int(line[1]))
答案 1 :(得分:0)
在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是必填字段,因此我在“未知”中对缺少的Title进行了硬编码。 PK = 1艺术家的名字叫Unknown。
答案 2 :(得分:0)
使用 Rakesh 和 ajwong4 anwsers 我想出了这个使用熊猫的通用解决方案
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()