Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import app model class in another app model

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!

like image 747
Lucas Rezende Avatar asked Dec 14 '12 13:12

Lucas Rezende


People also ask

What is a circular import in Django?

Python Circular Imports is a type of Circular dependency. It occurs in python when two or more models import each other and it repeats the importing connection into an infinite circular call. With Circular Imports, the python script gives an error.

What file do we use to create models in a Django app?

First we need to open and edit the models.py file so that it contains the code for generating a Post model. A Post model contains the following database fields: title — The title of the blog post. slug — Where valid URLs are stored and generated for web pages.


1 Answers

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.

like image 149
kobuz Avatar answered Sep 21 '22 04:09

kobuz