Django auth模型错误:未定义名称“User”

时间:2022-04-09 19:18:36

1. The error codes:

1.错误代码:

NameError: name 'User' is not defined

NameError:未定义名称“用户”

>>> myproject ME$ python manage.py shell
NameError: name 'User' is not defined
myproject ME$ python manage.py shell
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
>>> user.is_staff = True
>>> user.save()
>>> User.objects.all()
[<User: superuser>, <User: john>]
>>> from django.db import models
>>> from django.contrib.auth.models import User
>>> 
>>> 
>>> from django.conf import settings
>>> user = models.ForeignKey(settings.AUTH_USER_MODEL)
>>> User.objects.all()
[<User: superuser>, <User: john>]
>>> user.save()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'ForeignKey' object has no attribute 'save'
>>> from django.conf import settings

Do you have suggestions?

你有什么建议吗?

What can I do?

我能做什么?

Is there a way to check whether the user auth model was imported? If yes, what can I do in case it was not correctly imported?

有没有办法检查是否导入了用户身份验证模型?如果是,如果未正确导入,我该怎么办?

Is there a problem with my bookmarks app?

我的书签应用程序有问题吗?

2. My models.py:

2.我的models.py:

from django.db import models

class Bookmark(models.Model):
    author = models.ForeignKey(User)
    title = models.CharField(max_length=200, blank=True, default="")
    url = models.URLField()
    timestamp = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return "%s by %s" % (self.url, self.author.username)

class Tag(models.Model):
    bookmarks = models.ManyToManyField(Bookmark)
    slug = models.CharField(max_length=50, unique=True)

    def __unicode__(self):
        return self.slug

1 个解决方案

#1


15  

You are referencing User here: author = models.ForeignKey(User), but forgot to import User in your models.py

您在这里引用用户:author = models.ForeignKey(User),但忘记在models.py中导入User

Put this at the top of your models.py file

将它放在models.py文件的顶部

from django.contrib.auth.models import User

EDIT:

An alternate (newer) way of importing user model - for configurable user models would be:

导入用户模型的另一种(更新的)方式 - 对于可配置的用户模型,将是:

from django.contrib.auth import get_user_model
User = get_user_model()

The configured User model returned would be based on the setting AUTH_USER_MODEL

返回的已配置用户模型将基于设置AUTH_USER_MODEL

#1


15  

You are referencing User here: author = models.ForeignKey(User), but forgot to import User in your models.py

您在这里引用用户:author = models.ForeignKey(User),但忘记在models.py中导入User

Put this at the top of your models.py file

将它放在models.py文件的顶部

from django.contrib.auth.models import User

EDIT:

An alternate (newer) way of importing user model - for configurable user models would be:

导入用户模型的另一种(更新的)方式 - 对于可配置的用户模型,将是:

from django.contrib.auth import get_user_model
User = get_user_model()

The configured User model returned would be based on the setting AUTH_USER_MODEL

返回的已配置用户模型将基于设置AUTH_USER_MODEL