英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论