Class没有对象成员。

25 浏览
0 Comments

Class没有对象成员。

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”没有“objects”成员

我正在按照 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)的警告。

我在虚拟环境中运行了该命令,但还是无济于事。

所以,任何帮助都将是宝贵的。

admin 更改状态以发布 2023年5月23日
0
0 Comments

@tieuminh2510的回答非常完美。 但是在更新版本的VSC中,您将找不到在中编辑或粘贴该命令的选项。

对于更新版本,请按以下步骤添加代码:

  1. ctrl + shift + p打开命令面板。
  2. 现在在命令面板中输入Preferences: Configure Language Specific Settings
  3. 选择Python
  4. 将这些代码行添加到第一个花括号内:

    "python.linting.pylintArgs": [
            "--load-plugins=pylint_django",
        ]

确保已经安装了pylint-django

0
0 Comments

使用pip按如下方式安装pylint-django

pip install pylint-django

然后在Visual Studio Code中转到:用户设置Ctrl + ,或File>Preferences>Settings(如果可用)),输入以下内容(请注意,VSC中需要自定义用户设置的花括号是必须的):

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

0