Visual Studio Code – VSCode – Pylint(E5142:imported-auth-user)

huangapple go评论58阅读模式
英文:

Visual Studio code - vscode - Pylint(E5142:imported-auth-user)

问题

在Django项目的models.py文件中进行以下操作:

from django.contrib.auth.models import User
from django.db import models

class Pizza(models.Model):
    """客户可以订购的披萨。"""
    name = models.CharField(max_length=250)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        """返回模型的字符串表示。"""
        return str(self.name)

我得到了以下Pylint错误:

> 从django.contrib.auth.models导入的用户模型
> Pylint(E5142:imported-auth-user)

为什么Pylint会引发这个错误?尽管一切似乎都配置正确。虚拟环境、Python解释器、pylint-django...

提前感谢...

英文:

Doing the following in models.py from a Django Project:

from django.contrib.auth.models import User
from django.db import models

class Pizza(models.Model):
    """A pizza that our clients can order."""
    name = models.CharField(max_length=250)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        """Return a string representation of the model."""
        return str(self.name)

I got the following pylint error:

> User model imported from django.contrib.auth.models
> Pylint(E5142:imported-auth-user)

Why is Pylint rising this error? Despite the fact that everything seems to be configured right. Virtual env, python interpreter, pylint-django...

Thanks in advance...

答案1

得分: 0

我通过以下方式修复了它:

from django.contrib.auth import models as auth_models
from django.db import models

class Pizza(models.Model):
    """A pizza that our clients can order."""
    name = models.CharField(max_length=250)
    owner = models.ForeignKey(auth_models.User, on_delete=models.CASCADE)

    def __str__(self):
        """Return a string representation of the model."""
        return str(self.name)

以与Django的数据库模型相同的方式导入django.contrib.auth.models

英文:

I fixed it doing the following:

from django.contrib.auth import models as auth_models
from django.db import models

class Pizza(models.Model):
    """A pizza that our clients can order."""
    name = models.CharField(max_length=250)
    owner = models.ForeignKey(auth_models.User, on_delete=models.CASCADE)

    def __str__(self):
        """Return a string representation of the model."""
        return str(self.name)

Importing django.contrib.auth.models in the same way as Django does with its db models.

huangapple
  • 本文由 发表于 2023年5月24日 19:37:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定