Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django -- can't import my own models

I have several Django apps, all within one project directory. Each app has a models.py file with a bunch of models. I have been importing models from one app to the other with no problem, specifically to create a new model with a foreign key field, pointing to the model in the other app. No problem so far.

I decided to create a new model. It has 4 ForeignKey fields, each pointing to a different model in a different app. Straight forward. However, when I try to use south to migrate the schema to the database, it tells me that it can't import my models. Why?!

So, this is my new model:

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']

At the top of this models.py file I have the following imports:

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

From what I can tell, I am importing these correctly. However, when I try to do a schema migration, I get the following error:

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

Any idea what is going wrong here??

Thanks

EDIT -- Upon Request

I'm posting the work_order models.py code as requested (at least, the relevant parts):

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']
like image 626
Garfonzo Avatar asked Dec 27 '22 12:12

Garfonzo


1 Answers

If you read through the ForeignKey documentation, you'll see that the first argument can be a string. This allows you to create your foreign keys without having to import, getting around the circular reference problem.

So your model would then become:

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']

No import statements required!

like image 110
Evan Brumley Avatar answered Jan 04 '23 12:01

Evan Brumley