【发布时间】:2021-05-24 03:33:11
【问题描述】:
我最近一直在从事一个项目,该项目只是将数据从 csv 文件解析到存储在 PostgreSQL 数据库中的 django 模型。只要我注释掉解析函数,运行“makemigrations”和“迁移”就可以了,但是当我使用该函数运行服务器时,现在取消注释错误“django.db.utils.ProgrammingError:关系“database_class”不存在”。
代码:
import csv
import os
import time
import json
from django.db import models
from django.core import serializers
from django.contrib.postgres.fields import ArrayField
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Individual class model
class Class(models.Model):
prefix = models.CharField(max_length=10)
ID = models.IntegerField(null=False, blank=False)
name = models.TextField(null=False, blank=False)
description = models.TextField(null=False, blank=False)
HI = models.IntegerField(null=False, blank=False, default=0)
CI = models.IntegerField(null=False, blank=False, default=0)
DI = models.IntegerField(null=False, blank=False, default=0)
fall = models.IntegerField(null=False, blank=False, default=0)
spring = models.IntegerField(null=False, blank=False, default=0)
summer = models.IntegerField(null=False, blank=False, default=0)
pathways = ArrayField(models.CharField(max_length=150, blank=True), default=list, size=10, null=True, blank=True)
def __str__(self):
return self.name
#Parse through CSV file
def parse():
# Get the absolute csv file
csvFile = os.getcwd() + "/" + "HassPathways.csv"
# Open the file
with open(csvFile, 'r') as file:
# Read it and note the delimeter
file_r = csv.reader(file, delimiter=',')
# Loop through teh columns
for col in file_r:
# Skip the empty empty data (work on more debugging so we can handle errors, or maybe create a Google app script that handles the errors for us... more details on that later)
if (col[0] == "" or col[6] == "" or col[1] == "ID"):
continue
# Make sure the class object doesn't already exist
if (len(Class.objects.get(prefix__exact = col[0].strip(), ID__exact = col[1].strip() ,name__exact = col[2].strip())) == 0):
print("Creating " + col[2].strip())
# Create class
created = Class.objects.create(
prefix = col[0].strip(),
ID = col[1].strip(),
name = col[2].strip(),
description = col[3].strip(),
HI = col[4].strip(),
CI = col[5].strip(),
DI = col[6].strip(),
fall = col[7].strip(),
spring = col[8].strip(),
summer = col[9].strip())
created.pathways.append(col[10].strip())
# Save class
created.save()
else:
print("Adding to " + col[2].strip())
result = Class.objects.get(prefix__exact = col[0].strip(), ID__exact = col[1].strip() ,name__exact = col[2].strip())
if (col[10].strip() in result.pathways):
continue
else:
result.pathways.append(col[10].strip())
result.save()
#Main code start
parse()
print("Done!")
【问题讨论】: