【发布时间】:2017-12-21 11:04:09
【问题描述】:
def index(request):
latest_question_list = Question.objects.all().order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {'latest_question_list':latest_question_list}
return HttpResponse(template.render(context, request))
该函数的第一行在Question.objects.all() 上出现错误:
E1101:“问题”类没有“成员”对象
我正在关注 Django 文档教程,他们已经启动并运行了相同的代码。
我已经尝试调用一个实例。
Question = new Question()
and using MyModel.objects.all()
我的models.py 代码也是这个......
class Question(models.Model):
question_text = models.CharField(max_length = 200)
pub_date = models.DateTimeField('date published')
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
def __str__(self):
return self.question_text
无济于事,我仍然有这个错误。
我已经阅读了 pylint 并运行了这个...
pylint --load-plugins pylint_django
这并没有帮助,即使 github 自述文件说...
防止有关 Django 生成的属性的警告,例如 Model.objects 或 Views.request。
我在我的 virtualenv 中运行了这个命令,但什么也没有。
所以任何帮助都会很棒。
【问题讨论】:
-
使用
pylint --generated-members=objects -
抱歉,在我的 cmd 中运行它并没有改变任何东西。
-
你的python和django版本是多少?
-
版本 python 3.6.0 和 django 1.11.3
-
对我来说,解决问题的方法是在
models.py文件中创建一个Manager实例。请参阅django documentation 了解更多信息。
标签: python django django-views