gpt4 book ai didi

django - 在另一个应用程序模型中导入应用程序模型类

转载 作者:行者123 更新时间:2023-12-04 02:56:21 25 4
gpt4 key购买 nike

我有 2 个应用程序:同事服务 ,每个都有自己的models.py

在同事models.py 中,我可以“从services.models 导入服务”。

当我尝试在 servicesmodels.py 中“从 coworkers.models 导入状态”时,我收到此错误消息:

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



——

同事模型.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

服务模型.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

——
有人可以帮我看看我做错了什么吗?

提前致谢!

最佳答案

在 Django 1.6.5 中:

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

应该是这样的:
import coworkers
status = models.ForeignKey(coworkers.Status)

我(现在)知道 OP 正在使用 Django 1.4.3,并且某些答案可能会在该版本的 Django 中解决这个问题。但是,我花了一段时间才注意到版本,这些答案在 1.6.5 中不起作用。

干杯!

关于django - 在另一个应用程序模型中导入应用程序模型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13879431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com