类没有对象成员

buu*_*ted 110 python django django-views

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))
Run Code Online (Sandbox Code Playgroud)

该函数的第一行在Question.objects.all() - > E1101:类'问题has no objects成员`上出错

我遵循Django文档教程,他们有相同的代码并运行.

我试过调用一个实例.

Question = new Question()
and using MyModel.objects.all()
Run Code Online (Sandbox Code Playgroud)

我的那个类的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
Run Code Online (Sandbox Code Playgroud)

无济于事我仍然有这个错误.

我读过有关pylint的内容并且运行了这个......

pylint --load-plugins pylint_django
Run Code Online (Sandbox Code Playgroud)

哪个没有帮助,即使是github自述文件说...

防止有关Django生成的属性(如Model.objects或Views.request)的警告.

我在virtualenv中运行命令,但什么都没有.

所以任何帮助都会很棒

小智 254

pylint-django使用pip如下安装

pip install pylint-django
Run Code Online (Sandbox Code Playgroud)

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

{"python.linting.pylintArgs": [
     "--load-plugins=pylint_django"
],}
Run Code Online (Sandbox Code Playgroud)

  • 优秀!我还要指出你可以将插件选项放在`.pylintrc`文件中,如下所示:`load-plugins = pylint_django`这样它也可以被CLI选中,所以它可以在持续集成设置. (16认同)
  • 这样做之后,我收到了 `[pylint] C0111:Missing module docstring` 错误 (2认同)
  • @VishnuSharma 通过添加配置。**它会禁用 Pylint 的默认值**,因此您应该手动添加默认值。您可以按如下方式执行此操作: `{"python.linting.pylintArgs": [ "--load-plugins=pylint_django", "--disable=all", "--enable=F,E,unreachable,duplicate-key ,不必要的分号,全局变量未分配,未使用的变量,二进制操作异常,格式错误的字符串,字符串中异常反斜杠,错误的打开模式" ]}` [了解更多信息](https://code.visualstudio.com/docs/python/linting#_commandline-arguments-and-configuration-files) (2认同)

Fig*_*ode 58

@ tieuminh2510答案很完美.但是在较新版本的VSC中,您将找不到在" 用户设置"中编辑或粘贴该命令的选项.现在在较新版本中添加该代码,请按照以下步骤操作:

ctr + sft + P打开命令调色板.现在在命令面板中键入Preferences:Configure Language Specific Settings.现在选择Python.右侧粘贴此代码

"python.linting.pylintArgs": [
        "--load-plugins=pylint_django",
    ]
Run Code Online (Sandbox Code Playgroud)

在第一个花括号内.确保pylint-django.

希望这会有所帮助!

  • 不要忘记在json文件中添加逗号,否则它将无法正常工作。 (3认同)
  • 它解决了之前的错误。但是,现在警告来了。```缺少模块 docstringpylint(missing-module-docstring)```。为此可以做什么呢? (3认同)
  • 同样不要忘记`pip install pylint-django`。如果未安装扩展程序,则对设置的更改似乎会悄然失败,破坏所有的皮棉。 (2认同)
  • 谢谢。您可以在此处的官方 VS Code 文档中找到相同的信息:https://code.visualstudio.com/docs/python/linting#_commandline-arguments-and-configuration-files (2认同)

use*_*966 44

安装 Django pylint:

pip install pylint-django
Run Code Online (Sandbox Code Playgroud)

ctrl+shift+p > 首选项:配置特定于语言的设置 > Python

可用于 Python 语言的 settings.json 应如下所示:

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

    "[python]": {

    }
}
Run Code Online (Sandbox Code Playgroud)


ame*_*ina 23

UPDATE FOR VS CODE 1.40.0

After doing:

$ pip install pylint-django
Run Code Online (Sandbox Code Playgroud)

Follow this link: https://code.visualstudio.com/docs/python/linting#_default-pylint-rules

Notice that the way to make pylint have into account pylint-django is by specifying:

"python.linting.pylintArgs": ["--load-plugins", "pylint_django"]
Run Code Online (Sandbox Code Playgroud)

in the settings.json of VS Code.

But after that, you will notice a lot of new linting errors. Then, read what it said here:

只要将python.linting.pylintUseMinimalCheckers设置为true(默认值),就会传递这些参数。如果您在pylintArgsPylint 配置文件中指定值或使用 Pylint 配置文件(请参阅下一节),则pylintUseMinimalCheckers隐式设置为 false

我所做的是.pylintrc按照链接中的描述创建一个文件,然后在文件中配置以下参数(保留文件的其余部分不变):

load-plugins=pylint_django

disable=all

enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode
Run Code Online (Sandbox Code Playgroud)

现在pylint按预期工作。

  • 您可以通过在 settings.json 中执行此操作来获得相同的效果,而不是创建单独的 .pylintrc 文件: { "python.linting.pylintArgs": [ "--load-plugins=pylint_django", "--disable=all ", "--enable=F,E,无法访问,重复键,不必要的分号,全局变量未分配,未使用的变量,二进制操作异常,格式错误的字符串,异常反斜杠-string,坏打开模式"] } (3认同)

buu*_*ted 19

这是答案.来自我的reddit帖子... https://www.reddit.com/r/django/comments/6nq0bq/class_question_has_no_objects_member/

这不是错误,只是来自VSC的警告.Django动态地将属性添加到所有模型类中(它在引擎盖下使用了很多魔法),因此IDE不会通过查看类声明来了解它,因此它会警告您可能出现的错误(事实并非如此).对象实际上是一个帮助查询数据库的Manager实例.如果你真的想摆脱那个警告,你可以去你的所有模型并添加对象= models.Manager()现在,VSC将看到声明的对象,并且不会再次抱怨它.

  • 模型类中的`objects = models.Manager()`在VS Code中为我做了.谢谢. (9认同)

LeR*_*Roy 9

只需添加@Mallory-Erik 所说的内容:您可以将objects = models.Manager()它放在模态中:

class Question(models.Model):
    # ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    # ...
    def __str__(self):
        return self.question_text
    question_text = models.CharField(max_length = 200)
    pub_date = models.DateTimeField('date published')
    objects = models.Manager()
Run Code Online (Sandbox Code Playgroud)

  • 这是唯一适用于我的 Django 3 和 Python 3.8 的解决方案。谢了哥们。 (2认同)

Wil*_*oho 9

我已经尝试了提供的所有可能的解决方案,但是不幸的是,我的vscode设置不会更改其linter路径。因此,我尝试在设置>用户设置> python中探索vscode设置。查找Linting:Pylint路径,并将其更改为“ pylint_django”。别忘了在设置>用户设置> python配置中将linter更改为“ pylint_django”,从“ pyLint”更改为“ pylint_django”。

林特路径编辑

  • 在 MacOS High Sierra 和 VSC 1.38 上为我工作,前提是我之前在虚拟环境中运行了“pip install pylint-django”。 (2认同)

mot*_*oth 8

您可以为Visual Studio Code更改Python扩展的linter.

在VS中打开命令选项板Ctrl + Shift + P并键入以下命令之一:

Python:选择Linter

当你选择一个linter它将被安装.我试过flake8,似乎问题解决了我.

  • 虽然这确实有效,但我发现 flake8 作为 linter 确实很烦人。 (2认同)

Ven*_*ari 6

将您的linter更改为 - flake8,问题就会消失。


Kin*_*mah 5

我能够更新用户 settings.json

在我的 Mac 上,它存储在:

~/Library/Application Support/Code/User/settings.json
Run Code Online (Sandbox Code Playgroud)

Within it, I set the following:

{
    "python.linting.pycodestyleEnabled": true,
    "python.linting.pylintEnabled": true,
    "python.linting.pylintPath": "pylint",
    "python.linting.pylintArgs": ["--load-plugins", "pylint_django"]
}
Run Code Online (Sandbox Code Playgroud)

That solved the issue for me.


voo*_*ger 1

通过这样做Question = new Question()(我认为这new是一个拼写错误),您将用 实例覆盖 Question 模型Question。就像 Sayse 在评论中所说:不要为变量使用与模型名称相同的名称。所以将其更改为类似的东西my_question = Question()