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

我有下面的文件,它是名为projectmanager的django项目的一部分,这个文件是projectmanager/projects/models.py。每当我使用python解释器导入一个项目来测试其功能时,我会得到第8行的一个名称错误,即找不到FileRepo()。如何正确导入这些类?理想情况下,我要寻找的是每个项目包含多个文件回购,每个包含和未知数量的文件。感谢您的帮助。

#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

1个回答

  1. 虽然McPeterson的基本观点是正确的,但是要找到一个名字,必须在上面定义它的使用位置,在您的情况下,这是没有帮助的。在Django中,不能任意将类分配为其他类的属性。你需要在他们之间定义适当的关系。我建议你阅读关于关系领域的文档。

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>