小编典典

类没有对象成员

django

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:类'Question has no objectsmember '上获取错误。

我遵循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中运行了命令,但是什么也没有。

所以任何帮助都会很棒


阅读 639

收藏
2020-03-27

共1个答案

小编典典

pylint-django使用pip如下安装

pip install pylint-django

然后在Visual Studio Code中转到:用户设置(Ctrl+ ,或文件>首选项>设置(如果可用))放入以下内容(请注意VSC中自定义用户设置所需的花括号):

{"python.linting.pylintArgs": [
     "--load-plugins=pylint_django"
],}
2020-03-27