Django - 无法导入我自己的模型

时间:2011-10-20 22:41:54

标签: django django-models database-schema

我有几个Django应用程序,都在一个项目目录中。每个应用都有一个带有一堆模型的models.py文件。我一直在将模型从一个应用程序导入到另一个应用程序,没有任何问题,特别是创建一个带有外键字段的新模型,指向另一个应用程序中的模型。到目前为止没问题。

我决定创建一个新模型。它有4个ForeignKey字段,每个字段指向不同应用中的不同模型。直接前进。但是,当我尝试使用south将架构迁移到数据库时,它告诉我它无法导入我的模型。为什么呢?!

所以,这是我的新模式:

class Action_Tracker(models.Model):
    dateOfAction = models.DateField(verbose_name = 'Date of Action')
    user = models.CharField(max_length=30, verbose_name = 'Action completed by')
    sys = models.ForeignKey(System, verbose_name='For System')
    wo = models.ForeignKey(Work_Order, verbose_name='Associated WO', blank=True, null=True)
    inv = models.ForeignKey(Invoice, verbose_name = 'Associated Invoice', blank=True, null=True)
    subdT = models.ForeignKey(SUBD_Tracker, verbose_name = 'Associated SUBD Tracker', blank=True, null=True)
    notes = models.TextField(verbose_name='Notes of Action', blank=True, null=True)

    def __unicode__(self):
        return u'%s -- %s' % (self.dateOfAction, self.notes)

    class Meta:
        orering = ['dateOfAction']

在此models.py文件的顶部,我有以下导入:

from django.db import models
from django.forms import ModelForm, forms
from staff.models import Employee, Position
from work_orders.models import Work_Order, SUBD_Tracker
from invoices.models import Invoice

据我所知,我正确导入这些内容。但是,当我尝试进行模式迁移时,出现以下错误:

File "/srv/www/cpm/../cpm/systems/models.py", line 4, in <module>
  from work_orders.models import Work_Order, SUBD_Tracker
File "/srv/www/cpm/work_orders/models.py", line 5, in <module>
  from systems.models import System
File "/srv/www/cpm/systems/models.py", line 4, in <module>
  from work_orders.models import Work_Order, SUBD_Tracker
ImportError: cannot import name Work_Order

知道这里出了什么问题吗?

由于

编辑 - 根据要求

我按要求发布了work_order models.py代码(至少是相关部分):

from django.db import models
from django.forms import ModelForm, forms
from django import forms
from products.models import Product
from systems.models import System
from labour.models import Labour_Costs
from staff.models import Employee
from datetime import date
import datetime

class Work_Order(models.Model):
    IS_COMPLETE_CHOICES = (
        ('Y', 'Yes'),
        ('N', 'No'),
    )

    INVOICE_CREATED_CHOICES = (
        ('Y', 'Yes, mark WO complete and generate invoice (if applicable)'),
        ('N', 'No, just save these changes'),
    )

    WO_TYPE_CHOICES = (
        ('M', 'Mechanical'),
        ('I', 'Install'),
        ('S', 'Show'),
        ('R', 'Service'),
        ('B', 'Blow down'),
        ('U', 'Start up'),
    )

    woID = models.CharField(max_length = 25, primary_key = True, verbose_name = 'Work Order ID')
    woType = models.CharField(max_length = 2, verbose_name = 'WO Type', default='R', choices = WO_TYPE_CHOICES)
    systemID = models.ForeignKey(System, verbose_name = 'System ID')
    notesToCrew = models.TextField(blank = True, null = True, verbose_name = 'Notes to Crew')
    dateWOCreated = models.DateField(blank = True, null = True, default=datetime.date.today(), verbose_name = 'Date Created')
    dateWORequired = models.DateField(blank = True, null = True, verbose_name = 'Date Required')
    dateCompleted = models.DateField(blank = True, null = True, verbose_name = 'Date Completed')
    numDays = models.DecimalField(max_digits = 3, decimal_places = 0, verbose_name = 'Number of Days to Complete', blank = True, null = True)
    numHours = models.DecimalField(max_digits = 3, decimal_places = 2, verbose_name = 'Number of Hours to Complete', blank = True, null = True)
    isComplete = models.CharField (max_length = 3, default = 'N', verbose_name = 'Set WO as Completed?', choices = IS_COMPLETE_CHOICES)
    isScheduled = models.CharField (max_length = 3, default = 'N', verbose_name = 'Is WO scheduled?', choices = IS_COMPLETE_CHOICES)
    isReqSoon = models.BooleanField(default=False, verbose_name='Is WO Required soon') #This flag will be set in a function to indicate that the WO is reqd within the current week/few days, whatever
    problemDescription = models.TextField(verbose_name = 'Problem Description', blank = True, null = True)
    resolution = models.TextField(verbose_name = 'Resolution', blank = True, null = True)
    serviceFromBD = models.TextField(verbose_name = 'Service issues noticed', blank = True, null = True)
    serviceFromBDEstTime = models.DecimalField(max_digits = 3, decimal_places = 2, verbose_name = 'Est. Time to Repair', blank = True, null = True)
    numWorkers = models.DecimalField(max_digits = 3, default=1, decimal_places = 0, verbose_name = 'Number of Workers Required', blank = True, null = True)
    invoiceCreated = models.CharField (max_length = 3, default = 'N', verbose_name = 'Generate Invoice?', choices = INVOICE_CREATED_CHOICES)

    def __unicode__(self):
        return u'%s - %s' % (self.woID, self.systemID.systemAddress)

    class Meta:
        ordering = ['woID']


class SUBD_Tracker(models.Model):
    SUBD_CHOICES = (
        ('U', 'Startup'),
        ('B', 'Blowdown'),
    )

    sys = models.ForeignKey(System, verbose_name = 'Related System')
    subd = models.CharField(max_length=1, verbose_name = 'SU or BD', choices = SUBD_CHOICES)
    ssn = models.CharField(max_length=5, verbose_name = 'Season')
    approved = models.BooleanField(verbose_name = 'Approved', default = False)
    declined = models.BooleanField(verbose_name = 'Declined', default = False)
    cancelled = models.BooleanField(verbose_name = 'Cancel', default = False)
    firstNoticeSent = models.BooleanField(verbose_name = 'First Notice Sent?', default = False)
    secondNoticeSent = models.BooleanField(verbose_name = 'Second Notice Sent?', default = False)
    wo = models.ForeignKey(Work_Order, verbose_name = 'Related WO', blank = True, null = True)

    def __unicode__(self):
        if self.subd == 'U':
            return u'%s: Startup - %s' % (self.sys, self.ssn)
        else:
            return u'%s: Blowdown - %s' % (self.sys, self.ssn)

    class Meta:
        ordering = ['sys']

1 个答案:

答案 0 :(得分:10)

如果您通读ForeignKey documentation,您会看到第一个参数可以是一个字符串。这允许您创建外键而无需导入,解决循环引用问题。

所以你的模型会变成:

class Action_Tracker(models.Model):
    dateOfAction = models.DateField(verbose_name = 'Date of Action')
    user = models.CharField(max_length=30, verbose_name = 'Action completed by')
    sys = models.ForeignKey('systems.System', verbose_name='For System')
    wo = models.ForeignKey('work_orders.Work_Order', verbose_name='Associated WO', blank=True, null=True)
    inv = models.ForeignKey('invoices.Invoice', verbose_name = 'Associated Invoice', blank=True, null=True)
    subdT = models.ForeignKey('work_orders.SUBD_Tracker', verbose_name = 'Associated SUBD Tracker', blank=True, null=True)
    notes = models.TextField(verbose_name='Notes of Action', blank=True, null=True)

    def __unicode__(self):
        return u'%s -- %s' % (self.dateOfAction, self.notes)

    class Meta:
        ordering = ['dateOfAction']

无需导入声明!