当前位置:  开发笔记 > 编程语言 > 正文

AttributeError:'module'对象没有属性'model'

如何解决《AttributeError:'module'对象没有属性'model'》经验,为你挑选了2个好方法。

任何人都可以帮我解决这个问题..

from django.db import models

# Create your models here.
class Poll(models.model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()

运行:

c:\projects\mysite>python manage.py sql polls
Traceback (most recent call last):
  File "manage.py", line 11, in 
    execute_manager(settings)
  File "C:\Python25\Lib\site-packages\django\core\management\__init__.py", line 340, in execute_manager
    utility.execute()
  File "C:\Python25\Lib\site-packages\django\core\management\__init__.py", line 295, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python25\Lib\site-packages\django\core\management\base.py", line 195, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python25\Lib\site-packages\django\core\management\base.py", line 221, in execute
    self.validate()
  File "C:\Python25\Lib\site-packages\django\core\management\base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "C:\Python25\lib\site-packages\django\core\management\validation.py", line 28, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "C:\Python25\lib\site-packages\django\db\models\loading.py", line 128, in get_app_errors
    self._populate()
  File "C:\Python25\lib\site-packages\django\db\models\loading.py", line 57, in _populate
    self.load_app(app_name, True)
  File "C:\Python25\lib\site-packages\django\db\models\loading.py", line 72, in load_app
    mod = __import__(app_name, {}, {}, ['models'])
  File "c:\projects\mysite\..\mysite\polls\models.py", line 4, in 
    class Poll(models.model):
AttributeError: 'module' object has no attribute 'model'

Baishampayan.. 69

它被称为models.Model而不是models.model(区分大小写).修复你的民意调查模型 -

class Poll(models.Model):
    question = models.CharField(max_length=200) 
    pub_date = models.DateTimeField('date published')

希望有帮助......



1> Baishampayan..:

它被称为models.Model而不是models.model(区分大小写).修复你的民意调查模型 -

class Poll(models.Model):
    question = models.CharField(max_length=200) 
    pub_date = models.DateTimeField('date published')

希望有帮助......


@MichaelDorst:因为这个问题被多次看到,这个答案是正确的,答案是不被接受的.在我看来,这是SO的一个缺陷,答案不能被社区评为正确.

2> samgichuru..:

我也有同样的错误,但我注意到我输入了外国*k*ey而不是外国*K*ey,(大写K)如果有新手,请查看拼写和上限.

推荐阅读
可爱的天使keven_464
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有