在其他应用模型中导入应用模型类 [英] Import app model class in another app model
问题描述
我有2个应用程序:同事和服务,每个都有自己的models.py
我可以从services.models导入服务。
当我在服务models.py中尝试从coworkers.models import Status获取此错误消息:
追溯(最近的最后一次呼叫):文件
/ 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)文件/Users/lucas/Documents/projetos/cwk-manager/lib/python2.7/site-packages/Django-1.4.3-py2.7.egg/django/core /management/base.py,
第266行,验证
num_errors = get_validation_errors(s,app)文件/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,i 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,第158行,get_app_errors
self._populate()文件/ Users / lucas / Documents / projetos / cwk-manager / lib / python2.7 / site-packages / Django-1.4.3-py2.7.egg / django / db / models / loading.py,第64行,在_populate
self.load_app(app_name,True)文件 /Users/lucas/Documents/projetos/cwk-manager/lib/python2.7/site-packages/Django-1.4.3-py2.7.egg/django/db/models/loading.py,第88行, load_app
models = import_module('。models',app_name)文件/Users/lucas/Documents/projetos/cwk-manager/lib/python2.7/site-packages/Django-1.4.3-py2.7
第35行,import_module
导入(名称)文件/ Users / lucas / Documents / projetos / cwk-manager /cwk-manager/cwk_manager/coworkers/models.py,
第2行,在
从services.models导入服务文件/Users/lucas/Documents/projetos/cwk-manager/cwk-manager/cwk_manager/services/models.py,
第5行,
class Services(models.Model):文件/Users/lucas/Documents/projetos/cwk-manager/cwk-manager/cwk_manager/services/models.py,
第11行,在Services
status = models.ForeignKey(Status)NameError:name'Status'未定义
-
coworker models.py
from django.db import models
from services.models import Services
class状态(models.Model):
status_name = models.CharField(max_length = 50)
status_description = models.TextField(blank = True,null = True)
class Meta:
verbose_name =状态
verbose_name_plural =状态
def __unicode__ (self):
return self.status_name
服务models.py
从django.db导入模型
从进程状态
#此表包含有关计划和其他一般服务的所有信息。
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
-
有人可以帮我看看我做错了什么?
提前感谢!
在Django 1.6.5这里:
进口同事
status = models.ForeignKey(coworkers.models.Status)
应该是这样的:
import joorkers
status = models.ForeignKey(coworkers.Status)$ b $我现在(现在)知道OP使用的是Django 1.4.3,而且一些答案可能会解决这个问题。在那个版本的Django。但是,花了一段时间才能注意到版本,而这些答案在1.6.5中不起作用。
干杯!
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!
解决方案 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!
这篇关于在其他应用模型中导入应用模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!