【发布时间】:2014-10-16 19:14:01
【问题描述】:
1.错误代码:
名称错误:未定义名称“用户”
>>> 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
你有什么建议吗?
我能做什么?
有没有办法检查用户授权模型是否被导入?如果是,如果没有正确导入,我该怎么办?
我的书签应用有问题吗?
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
【问题讨论】:
标签: python django django-models django-admin django-authentication