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

时间:2010-05-19 18:48:22

标签: python django import

我有下面的文件,它是一个名为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

2 个答案:

答案 0 :(得分:3)

虽然McPeterson一般来说,对于要找到的名称,它必须在上面定义它的位置,在你的情况下无济于事。在Django中,您不能随意将类分配为其他类的属性。您需要在它们之间定义适当的关系。我建议你阅读the documentation on relationship fields

答案 1 :(得分:0)

在你打电话之前你有没有声明过FileRepo? IE,在models.py文件中的类Project之前移动类FileRepo?