我是Django新手,有一个警告但我无法解决它

问题描述 投票:0回答:1

warningpng

from django.shortcuts import render
from django.http import HttpResponse
from .models import ArticlePost
def article_list(request):
    articles = ArticlePost.objects.all()
    context = {'articles': articles}
    return render(request,'article/list.html',context)

# warning is:Class 'ArticlePost' is an unresolved feature reference for the 'objects'

此外,还有models.py和setting.py: models.py](https://i.stack.imgur.com/UDrgP.png) [setting.py`

期待您的帮助。

我运行了

python manage.py check
,终端显示“系统检查未发现任何问题(0 已静音)。”

python django-views
1个回答
0
投票

这只是 PyCharm 不理解 Django 的元编程逻辑。事实上,如果您不手动指定管理器,

Model
(不是抽象的)的任何子类都包含一个名为
objects
的管理器。但这是通过
ModelBase
元类[GitHub]
:

完成的
    def _prepare(cls):
        # …
        if not opts.managers:
            manager = Manager()
            manager.auto_created = True
            cls.add_to_class("objects", manager)

但这对于简单的 IDE 来说通常有点太多了,因此无法理解元编程,因此一些 IDE 和代码验证器会报告警告或错误,表明该类没有名为

.objects
的属性。

有像

pylint-django
 [pypi.org] 这样的 linter,其中包含对此进行推理的规则。此外,PyCharm 有一个付费版本,其中包括 Django 支持,该版本添加了额外的规则,允许 IDE 推理 Django 模型。

© www.soinside.com 2019 - 2024. All rights reserved.