【问题标题】:django.db.utils.ProgrammingError: relation "database_class" does not existdjango.db.utils.ProgrammingError:关系“database_class”不存在
【发布时间】: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!")

【问题讨论】:

    标签: python django


    【解决方案1】:

    如果我是你。我会将parse 函数移动到帮助文件中以进行清理。

    然后你可以在你的助手中执行`from .models import Class。

    其次,我会将 Class 重命名为其他名称。

    第三,确保__init__.py 文件与您的模型在同一文件夹中。

    【讨论】:

    • 我制作了帮助文件,但问题仍然存在。 init.py 在那里,但里面什么都没有
    • 没错。 __init__.pys 通常为空。它用于将目录标记为 Python 包。
    • 我的迁移文件夹丢失了,谢谢您的帮助!
    • 很高兴你知道了!
    猜你喜欢
    • 2021-03-30
    • 1970-01-01
    • 2018-05-11
    • 2014-10-25
    • 2019-03-15
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多