【问题标题】:module object has no attribute after using different version of Python使用不同版本的 Python 后模块对象没有属性
【发布时间】:2017-08-05 17:11:49
【问题描述】:

我安装了 python 2.7 和 3.4 两个版本。我正在使用一些在 2.7 下开发的代码,但我在 3.4 下使用。所以编译后使用下面的命令

python manage.py runserver

我收到以下错误 -

  File "C:\pyprojects\focus\site\general\forms.py", line 26, in Meta
    model = models.UserProfile
AttributeError: 'module' object has no attribute 'UserProfile'

目录结构是

├───focus
│   ├───data_dumps
│   ├───notes
│   ├───setup
│   └───site(main project folder)
|       └───static
|       └───general
|           +--forms.py
|           +--models.py
|       └───pro
|           └───models
|               +--__init__.py
|               +--plans.py
│       └───focus2
│           └───templates
|           +--__init__.py
│           +--settings.py
│           +--util.py            
│           +--wsgi.py
│       +--manage.py

由于models.py和forms.py在同一个目录下(一般),所以我以这种方式在forms.py中导入了模型

from .models import models

现在在 models.py 中我已经定义了类

class UserProfile(models.Model, HashedPk):
        user = models.OneToOneField(User, unique=True)
        is_pro = models.BooleanField(default=False, blank=True)
        ......................................................

forms.py 中的代码是

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = models.UserProfile
        .....................

python 3.4中调用模型有什么特殊方法吗?

非常感谢任何帮助。

【问题讨论】:

  • 你在使用与 python2 和 python3 相同的 django 版本吗?
  • 是的,我使用的是相同的 django 版本,即 1.10

标签: python-2.7 python-3.x


【解决方案1】:

已排序。导入需要

from . import models

改为

from .models import models

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-14
    • 2015-03-09
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2017-07-20
    相关资源
    最近更新 更多