10

I got 2 app: coworkers and services, each one with its own models.py

In coworkers models.py, I can "from services.models import Services".

When I try to "from coworkers.models import Status" in services models.py, I get this error message:

Traceback (most recent call last): File "/Users/lucas/Documents/projetos/cwk-manager/lib/python2.7/site-packages/Django-1.4.3-py2.7.egg/django/core/management/commands/runserver.py", line 91, in inner_run self.validate(display_num_errors=True) File "/Users/lucas/Documents/projetos/cwk-manager/lib/python2.7/site-packages/Django-1.4.3-py2.7.egg/django/core/management/base.py", line 266, in validate num_errors = get_validation_errors(s, app) File "/Users/lucas/Documents/projetos/cwk-manager/lib/python2.7/site-packages/Django-1.4.3-py2.7.egg/django/core/management/validation.py", line 30, in get_validation_errors for (app_name, error) in get_app_errors().items(): File "/Users/lucas/Documents/projetos/cwk-manager/lib/python2.7/site-packages/Django-1.4.3-py2.7.egg/django/db/models/loading.py", line 158, in get_app_errors self._populate() File "/Users/lucas/Documents/projetos/cwk-manager/lib/python2.7/site-packages/Django-1.4.3-py2.7.egg/django/db/models/loading.py", line 64, in _populate self.load_app(app_name, True) File "/Users/lucas/Documents/projetos/cwk-manager/lib/python2.7/site-packages/Django-1.4.3-py2.7.egg/django/db/models/loading.py", line 88, in load_app models = import_module('.models', app_name) File "/Users/lucas/Documents/projetos/cwk-manager/lib/python2.7/site-packages/Django-1.4.3-py2.7.egg/django/utils/importlib.py", line 35, in import_module import(name) File "/Users/lucas/Documents/projetos/cwk-manager/cwk-manager/cwk_manager/coworkers/models.py", line 2, in from services.models import Services File "/Users/lucas/Documents/projetos/cwk-manager/cwk-manager/cwk_manager/services/models.py", line 5, in class Services(models.Model): File "/Users/lucas/Documents/projetos/cwk-manager/cwk-manager/cwk_manager/services/models.py", line 11, in Services status = models.ForeignKey(Status) NameError: name 'Status' is not defined

--

coworker models.py

from django.db import models
from services.models import Services

class Status(models.Model):
    status_name = models.CharField(max_length=50)
    status_description = models.TextField(blank=True, null=True)

    class Meta:

        verbose_name = "Status"
        verbose_name_plural = "Status"

    def __unicode__(self):
        return self.status_name

services models.py

from django.db import models
from coworkers.models import Status

# This table contains all the information about plans and other general services provided.
class Services(models.Model):
    service_name = models.CharField(max_length=100)
    service_description = models.TextField(blank=True, null=True)
    service_price = models.DecimalField(max_digits=7, decimal_places=2, blank=True, null=True)
    creation_date = models.DateField(auto_now_add=True)
    last_update = models.DateField(auto_now=True)
    status = models.ForeignKey(Status)

    class Meta: 

        verbose_name = "Services"
        verbose_name_plural = "Services"

    def __unicode__(self):
        return self.service_name

-- Can someone help me to see what I am doing wrong?

Thanks in advance!

Lucas Rezende
  • 2,516
  • 8
  • 25
  • 34
  • This is a fairly old question and django as evolved. https://stackoverflow.com/a/43847288/2644091 gives the tools to avoid import issues for newer versions of django (3th Note in the answer is the resolved the issue in this question for me). – dendragon Sep 30 '17 at 18:35

5 Answers5

12

This is caused by circular import in Python. You can use this syntax:

status = models.ForeignKey('coworkers.models.Status')

Django will determine model using this path so you don't need to import Status.

Another solution in your case would be to delete 2nd import statement in coworker.models because Services doesn't seem to be used in this file.

kobuz
  • 221
  • 1
  • 6
  • It is used in fact... I just didn't post the whole file! :) – Lucas Rezende Dec 14 '12 at 13:43
  • I understood but didn't work anyway... I changed it to a new field called "is_cative" as boolean and now it selved my problem for now (this issue is a little but urgent). Weird it didn't work as you said... =/ – Lucas Rezende Dec 14 '12 at 14:01
10

In Django 1.6.5 this:

import coworkers
status = models.ForeignKey(coworkers.models.Status)

Should be this:

import coworkers
status = models.ForeignKey(coworkers.Status)

I am (now) aware the the OP is using Django 1.4.3 and that some of the answers may solve this in that version of Django. However, it took me a while to notice the version and those answers do not work in 1.6.5.

Cheers!

e.thompsy
  • 1,297
  • 1
  • 15
  • 19
1

It's circularly import, which results errors.

You can try,

import coworkers
status = models.ForeignKey(coworkers.models.Status)
Qiang Jin
  • 4,427
  • 19
  • 16
  • 2
    So I can never import a model class in another app models.py if I have already imported this current model class in the previous models.py? :S This is really annoying... :) – Lucas Rezende Dec 14 '12 at 13:25
  • Now I got this: `File "/Users/lucas/Documents/projetos/cwk-manager/cwk-manager/cwk_manager/coworkers/models.py", line 2, in from services.models import Services File "/Users/lucas/Documents/projetos/cwk-manager/cwk-manager/cwk_manager/services/models.py", line 5, in class Services(models.Model): File "/Users/lucas/Documents/projetos/cwk-manager/cwk-manager/cwk_manager/services/models.py", line 11, in Services status = models.ForeignKey(coworkers.models.Status) AttributeError: 'module' object has no attribute 'models'` – Lucas Rezende Dec 14 '12 at 13:26
  • A import B, then B can not import A, otherwise it's ok. – Qiang Jin Dec 14 '12 at 13:27
  • try status = models.ForeignKey("coworkers.models.Status") or status = models.ForeignKey("coworkers.Status") – panchicore Dec 14 '12 at 13:30
  • well, it's still an import issue. i'm not quite sure what happened..i used to meet these errors, a quick and dirty solution is just put them in one file.. – Qiang Jin Dec 14 '12 at 13:32
0

Just create the Status model first and do syncdb and after that create the services model and syncdb. It should work.

The problem is that python is not ab le to find the 'Status' coz its first trying to create the Services model.

Saransh Mohapatra
  • 9,430
  • 10
  • 39
  • 50
0

I got all sorts of errors like this while doing syncdb:

CommandError: One or more models did not validate: parts.vehicle:
'customer' has a relation with model <class
'customers.models.Customer'>, which has either not been installed or
is abstract.

I finally realized I had forgotten to add the new app to settings.py. Admin couldn't find it either. That should have been my first clue. Otherwise I was doing what was in the answer by e.thompsy for django 1.6.5

Vicky T
  • 1,643
  • 4
  • 15
  • 12