Python 类没有对象成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45135263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class has no objects member
提问by buuencrypted
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: 类“问题”没有对象“成员”
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.
所以任何帮助都会很棒。
回答by voodoo-burger
By doing Question = new Question()
(I assume the new
is a typo) you are overwriting the Question model with an intance of Question
. Like Sayse said in the comments: don't use the same name for your variable as the name of the model. So change it to something like my_question = Question()
.
通过这样做Question = new Question()
(我认为这new
是一个错字),您正在使用Question
. 就像 Sayse 在评论中所说的那样:不要为您的变量使用与模型名称相同的名称。所以把它改成类似my_question = Question()
.
回答by buuencrypted
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. 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). objects is in fact a Manager instance that helps with querying the DB. 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.
这不是错误,只是来自 VSC 的警告。Django 动态地将该属性添加到所有模型类中(它在幕后使用了很多魔法),因此 IDE 不会通过查看类声明来了解它,因此它会警告您可能的错误(它不是)。objects 实际上是一个帮助查询数据库的 Manager 实例。如果您真的想摆脱该警告,您可以转到所有模型并添加 objects = models.Manager() 现在,VSC 将看到声明的对象并且不会再次抱怨它。
回答by tieuminh2510
Install pylint-django
using pip
as follows
安装pylint-django
使用pip
如下
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"
],}
回答by moth
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:选择 Linter
when you select a linter it will be installed. I tried flake8 and it seems issue resolved for me.
当您选择 linter 时,它将被安装。我试过 flake8,似乎问题已经解决了。
回答by LeRoy
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()
回答by Chirag Kalal
First install pylint-djangousing 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 代码所必需的
回答by Pritam Manerao
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_name' 没有 'member_name' 成员它可以通过以下方式在该行中被抑制:
# pylint: disable=no-member
回答by FightWithCode
@tieuminh2510 answer is perfect. But in newer versions of VSC you will not find thhe option to edit or paste that command in User Settings. Now in newer version to add that code follow this steps:
@tieuminh2510 答案是完美的。但是在较新版本的 VSC 中,您不会在用户设置中找到编辑或粘贴该命令的选项。现在在较新版本中添加该代码,请按照以下步骤操作:
Press ctr+sft+Pto open the the Command Palette. Now in command palette type Preferences: Configure Language Specific Settings. Now select Python. Here in right side paste this code
按ctr+sft+P打开命令面板。现在在命令面板中输入Preferences: Configure Language Specific Settings。现在选择Python。在右侧粘贴此代码
"python.linting.pylintArgs": [
"--load-plugins=pylint_django",
]
Inside the first curly braces. Make sure that pylint-django.
在第一个花括号内。确保pylint-django.
Hope this will help!
希望这会有所帮助!
回答by Willy satrio nugroho
I've tried all possible solutions offered but unluckly my vscode settings won't changed its linter path. So, I tride to explore vscode settings in settings > User Settings > python. Find Linting: Pylint Pathand change it to "pylint_django". Don't forget to change the linter to "pylint_django" at settings > User Settings > python configurationfrom "pyLint" to "pylint_django".
我已经尝试了所有可能的解决方案,但不幸的是我的 vscode 设置不会改变它的 linter 路径。因此,我尝试在settings > User Settings > python 中探索 vscode 设置。找到Linting: Pylint Path并将其更改为“pylint_django”。不要忘记在设置 > 用户设置 > python 配置中将 linter 更改为“pylint_django”,从“pyLint”到“pylint_django”。
回答by Venu Gopal Tewari
Change your linter to - flake8and problem will go away.
将您的linter更改为 - flake8,问题就会消失。