10
votes

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!

5
This is a fairly old question and django as evolved. 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

5 Answers

10
votes

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!

12
votes

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.

1
votes

It's circularly import, which results errors.

You can try,

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

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.

0
votes

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