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)
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.
希望这会有所帮助!
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
(默认值),就会传递这些参数。如果您在pylintArgs
Pylint 配置文件中指定值或使用 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
按预期工作。
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将看到声明的对象,并且不会再次抱怨它.
只需添加@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)
我已经尝试了提供的所有可能的解决方案,但是不幸的是,我的vscode设置不会更改其linter路径。因此,我尝试在设置>用户设置> python中探索vscode设置。查找Linting:Pylint路径,并将其更改为“ pylint_django”。别忘了在设置>用户设置> python配置中将linter更改为“ pylint_django”,从“ pyLint”更改为“ pylint_django”。
您可以为Visual Studio Code更改Python扩展的linter.
在VS中打开命令选项板Ctrl + Shift + P并键入以下命令之一:
Python:选择Linter
当你选择一个linter它将被安装.我试过flake8,似乎问题解决了我.
我能够更新用户 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.
通过这样做Question = new Question()
(我认为这new
是一个拼写错误),您将用 实例覆盖 Question 模型Question
。就像 Sayse 在评论中所说:不要为变量使用与模型名称相同的名称。所以将其更改为类似的东西my_question = Question()
。
归档时间: |
|
查看次数: |
59127 次 |
最近记录: |