python - 为什么我无法从另一个源文件导入 Django 模型?

标签 python django django-models

我尝试从 Django 应用程序导入模型并收到错误:

ImportError at /
cannot import name UserProfile
Request Method: GET
Request URL:    http://localhost:8000/
Django Version: 1.3.1
Exception Type: ImportError
Exception Value:    
cannot import name UserProfile
Exception Location: /Users/jonathan/pim/functions.py in <module>, line 2
Python Executable:  /usr/local/bin/python
Python Version: 2.7.0
Python Path:    
['/Users/jonathan/pim',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/pip-0.8.1-py2.7.egg',
 '/usr/local/Cellar/python/2.7/lib/python27.zip',
 '/usr/local/Cellar/python/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/usr/local/Cellar/python/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7/lib/python2.7/lib-old',
 '/usr/local/Cellar/python/2.7/lib/python2.7/lib-dynload',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/PIL',
 '/usr/local/lib/python2.7/site-packages',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
Server time:    Thu, 16 Feb 2012 12:14:07 -0600
Traceback Switch to copy-and-paste view

/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
                        request.path_info) ...
▶ Local vars
/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in resolve
            for pattern in self.url_patterns: ...
▶ Local vars
/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in _get_url_patterns
        patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) ...
▶ Local vars
/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in _get_urlconf_module
            self._urlconf_module = import_module(self.urlconf_name) ...
▶ Local vars
/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/utils/importlib.py in import_module
    __import__(name) ...
▶ Local vars
/Users/jonathan/pim/../pim/urls.py in <module>
import pim_calendar.views ...
▶ Local vars
/Users/jonathan/pim/../pim/pim_calendar/views.py in <module>
import functions ...
 ▶ Local vars
/Users/jonathan/pim/functions.py in <module>
from pim_accounts import UserProfile ...
▶ Local vars

我以前也遇到过类似的错误,我尝试导入模型并被告知不可能发生。有禁止进口模型的规定吗?有没有其他方法可以达到同样的目的?该项目位于http://JonathansCorner.com/project/pim.tgz .

最佳答案

functions.py中,更改行

from pim_accounts import UserProfile

from pim_accounts.models import UserProfile

关于python - 为什么我无法从另一个源文件导入 Django 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9317042/

相关文章:

python - Pygame:放置多个对象时出错:(

python - Django模板ifequal小数比较

python - 如何处理/映射自定义 postgresql 类型到 Django 模型

Django:我如何使用来自 django-extensions 的 UUIDField

django - 在 Django 中使用 through 查询多对多字段

python - django 异构查询集代理模型

python - 如何在 PyCharm 中禁用此示例程序?

python - 使用相同的键对过去的条目进行计数

python - 通过分组从 python 中的列表列表创建新列表

database - 加载大量初始数据时出现 MemoryError