Python PyCharm 中“类”的未解析属性引用“对象”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43865989/
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
Unresolved attribute reference 'objects' for class '' in PyCharm
提问by zhiang.shi
I use community pycharm and the version of python is 3.6.1, django is 1.11.1. This warning has no affect on running, but I cannot use the IDE's auto complete.
我用的是社区pycharm,python版本是3.6.1,django是1.11.1。此警告对运行没有影响,但我无法使用 IDE 的自动完成功能。
回答by vishes_shell
You need to enable Django support. Go to
您需要启用 Django 支持。去
PyCharm -> Preferences -> Languages & Frameworks -> Django
PyCharm -> 首选项 -> 语言和框架 -> Django
and then check Enable Django Support
然后检查 Enable Django Support
回答by Campi
You can also expose the default model manager explicitly:
您还可以显式公开默认模型管理器:
from django.models import models
class Foo(models.Model):
name = models.CharField(max_length=50, primary_key=True)
objects = models.Manager()
回答by winux
Python Frameworks (Django, Flask, etc.) are only supported in the Professional Edition. Check the link below for more details.
Python 框架(Django、Flask 等)仅在专业版中受支持。查看下面的链接了解更多详情。
回答by Christopher Davies
I found this hacky workaround using stub files:
我使用存根文件发现了这个hacky解决方法:
models.py
模型.py
from django.db import models
class Model(models.Model):
class Meta:
abstract = True
class SomeModel(Model):
pass
models.pyi
模型.pyi
from django.db import models
class Model:
objects: models.Manager()
This should enable PyCharm's code completion:
This is similar to Campi's solution, but avoids the need to redeclare the default value
这类似于 Campi 的解决方案,但避免了重新声明默认值的需要
回答by Joseph Bani
Use a Base model for all your models which exposes objects:
为所有暴露对象的模型使用基础模型:
class BaseModel(models.Model):
objects = models.Manager()
class Meta:
abstract = True
class Model1(BaseModel):
id = models.AutoField(primary_key=True)
class Model2(BaseModel):
id = models.AutoField(primary_key=True)
回答by Yarh
Another solution i found is putting @python_2_unicode_compatible decorator on any model. It also requires you to have a strimplementation four your function
我发现的另一个解决方案是将 @python_2_unicode_compatible 装饰器放在任何模型上。它还要求你有一个str实现四个你的函数
For example:
例如:
# models.py
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class SomeModel(models.Model):
name = Models.CharField(max_length=255)
def __str__(self):
return self.name