gpt4 book ai didi

python - 如何在Python中将类导入到同一文件中的其他类中

转载 作者:行者123 更新时间:2023-11-30 23:58:44 25 4
gpt4 key购买 nike

我有下面的文件,它是名为projectmanager的django项目的一部分,该文件是projectmanager/projects/models.py。每当我使用 python 解释器导入项目只是为了测试功能时,我都会收到第 8 行的名称错误,无法找到 FileRepo()。如何正确导入这些类?理想情况下,我正在寻找的是每个项目包含多个 FileRepos,每个 FileRepos 包含未知数量的文件。感谢您提前提供的任何帮助。

#imports
from django.db import models
from django.contrib import admin
#Project is responsible for ensuring that each project contains all of the folders and file storage
#mechanisms a project needs, as well as a unique CCL#
class Project(models.Model):
ccl = models.CharField(max_length=30)
Techpacks = FileRepo()
COAS = FileRepo()
Shippingdocs = FileRepo()
POchemspecs = FileRepo()
Internalpos = FileRepo()
Finalreports = FileRepo()
Batchrecords = FileRepo()
RFPS = FileRepo()
Businessdev = FileRepo()
QA = FileRepo()
Updates = FileRepo()

def __unicode__(self):
return self.ccl

#ProjectFile is the file object used by each FileRepo component
class ProjectFile(models.Model):
file = models.FileField(uploadto='ProjectFiles')

def __unicode__(self):
return self.file

#FileRepo is the model for the "folders" to be used in a Project
class FileRepo(models.Model):
typeOf = models.CharField(max_length=30)
files = models.ManyToManyField(ProjectFile)

def __unicode__(self):
return self.typeOf

最佳答案

尽管麦克彼得森总体上是正确的,为了找到一个名称,它必须在使用它的地方上面定义,但在你的情况下这没有帮助。在 Django 中,你不能任意将类指定为其他类的属性。您需要定义它们之间的适当关系。我建议您阅读the documentation on relationship fields .

关于python - 如何在Python中将类导入到同一文件中的其他类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2868418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com