python - 'OneToOneField' 的实例没有 'username' 成员

标签 python django

创建 Profile 模型时出现以下错误

Instance of 'OneToOneField' has no 'username' member

这是我创建的代码片段

from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default="default.jpg", upload_to="profile_pics")

    def __str__(self):
        return f"{self.user.username} Profile"

以前工作正常。现在,突然我收到此错误。我不明白此错误的含义。我该如何解决? 谢谢

最佳答案

为了让 pylint 能够与 Django 一起正常工作,您应该安装 pylint-django 包:

pip install pylint-django

然后您可以使用 pylint_django 作为插件运行 pylint:

pylint --load-plugins pylint_django <path_to_django_file>

如果您使用 VSCode 作为 IDE,则可以将此代码段添加到 .vscode/settings.json 文件中以加载项目的插件:

{
    "python.linting.pylintArgs": [
        "--load-plugins",
        "pylint_django"
    ]
}

或者,如果您有 .pylintrc 文件,则可以添加此行来加载插件:

[MASTER]
load-plugins=pylint_django

You can find out more on about pylint-django here.

关于python - 'OneToOneField' 的实例没有 'username' 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55204906/

相关文章:

Python:写入 Excel 2007+ 文件(.xlsx 文件)

python - 将字典映射标签分配给 pandas 中列的索引值

python - 我将如何使用 django.forms 使用模型中的行预填充选择字段?

python - 解析在 Django 中使用 Ajax GET 方法发送的 json 对象

Django 不会在 Postgres 上创建 ImageField

python - Django rest 框架序列化程序返回一个列表而不是 json

python - 可以在 pandas 数据框中创建子列吗?

python - 一个文件启动所有服务... mongodb、redis、node、angular 和 python

python - 装饰器类和缺少必需的位置参数

python - 如何动态地向 Django Model Admin readonly_fields 添加多个字段