[英]Class has no objects member
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))
The first line of that function gets an error on Question.objects.all()
:该函数的第一行在
Question.objects.all()
上出错:
E1101: Class 'Question' has no 'objects' member
E1101: 类 'Question' 没有 'objects' 成员
I'm following the Django documentation tutorial and they have the same code up and running.我正在关注 Django 文档教程,它们具有相同的代码并正在运行。
I have tried calling an instance.我试过调用一个实例。
Question = new Question()
and using MyModel.objects.all()
Also my models.py
code for that class is this...我那个类的
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
To no avail I still have this error.无济于事我仍然有这个错误。
I have read about pylint and ran this...我已经阅读了有关 pylint 的内容并运行了这个...
pylint --load-plugins pylint_django
Which didn't help, even tho the github readme file says...这没有帮助,即使 github 自述文件说......
Prevents warnings about Django-generated attributes such as Model.objects or Views.request.
防止有关 Django 生成的属性(例如 Model.objects 或 Views.request)的警告。
I ran the command within my virtualenv, and yet nothing.我在我的 virtualenv 中运行了命令,但什么也没有。
So any help would be great.所以任何帮助都会很棒。
Install pylint-django
using pip
as follows使用
pip
安装pylint-django
如下
pip install pylint-django
Then in Visual Studio Code goto: User Settings ( Ctrl + , or File > Preferences > Settings if available ) Put in the following (please note the curly braces which are required for custom user settings in VSC):然后在 Visual Studio Code 中转到:用户设置( Ctrl +或文件 > 首选项 > 设置(如果可用))输入以下内容(请注意 VSC 中自定义用户设置所需的大括号):
{"python.linting.pylintArgs": [
"--load-plugins=pylint_django"
],}
@tieuminh2510's answer is perfect. @tieuminh2510 的回答很完美。 But in newer versions of VSC you will not find the option to edit or paste that command in
User Settings
.但是在较新版本的 VSC 中,您将找不到用于在
User Settings
中编辑或粘贴该命令的选项。
For newer versions, add the code in the following steps:对于较新的版本,请按以下步骤添加代码:
Preferences: Configure Language Specific Settings
.Preferences: Configure Language Specific Settings
。Python
.Python
。 "python.linting.pylintArgs": [
"--load-plugins=pylint_django",
]
Make sure that
pylint-django
is also installed.确保还安装了
pylint-django
。
Install Django pylint:安装 Django pylint:
pip install pylint-django
ctrl+shift+p > Preferences: Configure Language Specific Settings > Python ctrl+shift+p > 首选项:配置语言特定设置 > Python
The settings.json available for python language should look like the below: python 语言可用的 settings.json 应该如下所示:
{
"python.linting.pylintArgs": [
"--load-plugins=pylint_django"
],
"[python]": {
}
}
I've tried all possible solutions offered but unluckly my vscode settings won't changed its linter path.我已经尝试了所有可能的解决方案,但不幸的是我的 vscode 设置不会改变它的 linter 路径。 So, I tride to explore vscode settings in settings > User Settings > python .
因此,我尝试在设置 > 用户设置 > python中探索 vscode 设置。 Find Linting: Pylint Path and change it to "pylint_django".
找到Linting:Pylint Path并将其更改为“pylint_django”。 Don't forget to change the linter to "pylint_django" at settings > User Settings > python configuration from "pyLint" to "pylint_django".
不要忘记在设置 > 用户设置 > python 配置中将 linter 更改为“pylint_django”,从“pyLint”到“pylint_django”。
Heres the answer.这是答案。 Gotten from my reddit post... https://www.reddit.com/r/django/comments/6nq0bq/class_question_has_no_objects_member/
来自我的 reddit 帖子... https://www.reddit.com/r/django/comments/6nq0bq/class_question_has_no_objects_member/
That's not an error, it's just a warning from VSC.
这不是错误,只是来自 VSC 的警告。 Django adds that property dynamically to all model classes (it uses a lot of magic under the hood), so the IDE doesn't know about it by looking at the class declaration, so it warns you about a possible error (it's not).
Django 将该属性动态添加到所有模型类(它在幕后使用了很多魔法),因此 IDE 通过查看类声明并不知道它,所以它会警告您可能的错误(它不是)。 objects is in fact a Manager instance that helps with querying the DB.
objects 实际上是一个帮助查询数据库的 Manager 实例。 If you really want to get rid of that warning you could go to all your models and add objects = models.Manager() Now, VSC will see the objects declared and will not complain about it again.
如果你真的想摆脱那个警告,你可以去你所有的模型并添加 objects = models.Manager() 现在,VSC 将看到声明的对象并且不会再次抱怨它。
UPDATE FOR VS CODE 1.40.0更新 VS 代码 1.40.0
After doing:做完之后:
$ pip install pylint-django
Follow this link: https://code.visualstudio.com/docs/python/linting#_default-pylint-rules点击此链接: 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:请注意,使
pylint
考虑到pylint-django
的方法是指定:
"python.linting.pylintArgs": ["--load-plugins", "pylint_django"]
in the settings.json
of VS Code.在 VS Code 的
settings.json
中。
But after that, you will notice a lot of new linting errors.但在那之后,你会注意到很多新的 linting 错误。 Then, read what it said here:
然后,阅读这里的内容:
These arguments are passed whenever the
python.linting.pylintUseMinimalCheckers
is set totrue
(the default).只要
python.linting.pylintUseMinimalCheckers
设置为true
(默认值),就会传递这些参数。 If you specify a value inpylintArgs
or use a Pylint configuration file (see the next section), thenpylintUseMinimalCheckers
is implicitly set to false .如果您在
pylintArgs
中指定一个值或使用 Pylint 配置文件(请参阅下一节),则pylintUseMinimalCheckers
将隐式设置为 false 。
What I have done is creating a .pylintrc
file as described in the link, and then, configured the following parameters inside the file (leaving the rest of the file untouched):我所做的是按照链接中的描述创建一个
.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
Now pylint
works as expected.现在
pylint
按预期工作。
You can change the linter for Python extension for Visual Studio Code.您可以为 Visual Studio Code 的 Python 扩展更改 linter。
In VS open the Command Palette Ctrl+Shift+P and type in one of the following commands:在 VS 中打开命令面板 Ctrl+Shift+P 并输入以下命令之一:
Python: Select Linter
Python:选择短绒
when you select a linter it will be installed.当您选择 linter 时,它将被安装。 I tried flake8 and it seems issue resolved for me.
我尝试了 flake8,似乎问题已解决。
Just adding on to what @Mallory-Erik said: You can place objects = models.Manager()
it in the modals:只是添加@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()
Change your linter to - flake8 and problem will go away.将 linter 更改为 - flake8 ,问题就会消失。
I was able to update the user settings.json我能够更新用户 settings.json
On my mac it was stored in:在我的 Mac 上,它存储在:
~/Library/Application Support/Code/User/settings.json
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"]
}
That solved the issue for me.这为我解决了这个问题。
First install pylint-django using following command首先使用以下命令安装pylint-django
$ pip install pylint-django
Then run the second command as follows:然后运行第二条命令如下:
$ pylint test_file.py --load-plugins pylint_django
--load-plugins pylint_django is necessary for correctly review a code of django --load-plugins pylint_django 是正确查看 django 代码所必需的
If you use python 3如果你使用 python 3
python3 -m pip install pylint-django
If python < 3如果蟒蛇 < 3
python -m pip install pylint-django==0.11.1
NOTE: Version 2.0, requires pylint >= 2.0 which doesn't support Python 2 anymore!注意:2.0 版需要 pylint >= 2.0,它不再支持 Python 2! ( https://pypi.org/project/pylint-django/ )
( https://pypi.org/project/pylint-django/ )
First, Install pylint-django
using pip
as follows:首先,使用
pip
安装pylint-django
,如下所示:
pip install pylint-django
Goto settings.json
find and make sure python linting enabled
is true
like this:转到
settings.json
查找并确保python linting enabled
是true
的:
At the bottom write "python.linting.pylintPath": "pylint_django"
like this:在底部写
"python.linting.pylintPath": "pylint_django"
像这样:
OR,要么,
Go to Settings
and search for python linting
转到
Settings
并搜索python linting
make sure Python > Linting: Pylint Enabled
is checked确保
Python > Linting: Pylint Enabled
被选中
Under that Python > Linting: Pylint Path
write pylint_django
在
Python > Linting: Pylint Path
写pylint_django
How about suppressing errors on each line specific to each error?如何抑制特定于每个错误的每一行的错误?
Something like this: https://pylint.readthedocs.io/en/latest/user_guide/message-control.html像这样的东西: https ://pylint.readthedocs.io/en/latest/user_guide/message-control.html
Error: [pylint] Class 'class_name' has no 'member_name' member It can be suppressed on that line by:错误:[pylint] Class 'class_name' has no 'member_name' member 它可以通过以下方式在该行上被抑制:
# pylint: disable=no-member
I installed PyLint but I was having the error Missing module docstringpylint(missing-module-docstring)
.我安装了 PyLint,但出现错误
Missing module docstringpylint(missing-module-docstring)
。 So I found this answer with this config for pylint:所以我用pylint的这个配置找到了这个答案:
"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
"--disable=C0111", // missing docstring
"--load-plugins=pylint_django,pylint_celery",
],
And now it works现在它起作用了
By doing Question = new Question()
(I assume the new
is a typo) you are overwriting the Question model with an intance of Question
.通过执行
Question = new Question()
(我假设new
是错字),您将用 Question 的实例覆盖Question
模型。 Like Sayse said in the comments: don't use the same name for your variable as the name of the model.正如 Sayse 在评论中所说:不要为变量使用与模型名称相同的名称。 So change it to something like
my_question = Question()
.因此,将其更改为类似
my_question = Question()
的内容。
This issue happend when I use pylint_runner当我使用 pylint_runner 时发生了这个问题
So what I do is open.pylintrc file and add this所以我所做的是打开.pylintrc 文件并添加这个
# List of members which are set dynamically and missed by pylint inference
# system, and so shouldn't trigger E1101 when accessed. Python regular
# expressions are accepted.
generated-members=objects
Just add objects = None in your Questions table.只需在您的问题表中添加 objects = None。 That solved the error for me.
这为我解决了错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.