我正在关注Tutorial 'Todo List' by lightbird
我认为我遇到的问题是让用户属性在models.py项目类中工作。我得到的错误似乎是由于我对添加用户属性定义的方向缺乏了解所致:
user = models.ForeignKey(User, blank=True, null=True)
我将这行代码添加到我的Item类中,但仍然没有User的参数。
这是我的完整models.py:
from django.db import models
from django.contrib import admin
from django.utils.translation import ugettext as _
from django.utils.encoding import force_unicode
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
# Create your models here.
class DateTime(models.Model):
datetime = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return unicode(self.datetime.strftime("%b %d, %Y, %I:%M %p"))
class Item(models.Model):
progress = models.IntegerField(default=0)
name = models.CharField(max_length=60)
created = models.ForeignKey(DateTime)
priority = models.IntegerField(default=0)
difficulty = models.IntegerField(default=0)
done = models.BooleanField(default=False)
user = models.ForeignKey(User, blank=True, null=True)
def mark_done(self):
return "<a href='%s'>Done</a>" % reverse("todo.views.mark_done", args=[self.pk])
mark_done.allow_tags = True
def progress_(self):
return "<div style='width: 100px; border: 1px solid #ccc;'>" + \
"<div style='height: 4px; width: %dpx; background: #555; '></div></div>" % self.progress
progress_.allow_tags = True
class ItemAdmin(admin.ModelAdmin):
list_display = ["name", "priority", "difficulty", "created", "done"]
search_fields = ["name"]
class ItemInline(admin.TabularInline):
model = Item
class DateAdmin(admin.ModelAdmin):
list_display = ["datetime"]
inlines = [ItemInline]
def response_add(self, request, obj, post_url_continue='../%s/'):
""" Determines the HttpResponse for the add_view stage. """
for item in Item.objects.filter(created=obj):
if not item.user:
item.user = request.user
item.save()
return HttpResponseRedirect(reverse("admin:todo_item_changelist"))
opts = obj._meta
pk_value = obj._get_pk_val()
msg = "Item(s) were added successfully."
# Here, we distinguish between different save types by checking for
# the presence of keys in request.POST.
if request.POST.has_key("_continue"):
self.message_user(request, msg + ' ' + _("You may edit it again below."))
if request.POST.has_key("_popup"):
post_url_continue += "?_popup=1"
return HttpResponseRedirect(post_url_continue % pk_value)
if request.POST.has_key("_popup"):
return HttpResponse(
'<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");'
'</script>' % (escape(pk_value), escape(obj)))
elif request.POST.has_key("_addanother"):
self.message_user(request, msg + ' ' + (_("You may add another %s below.") %
force_unicode(opts.verbose_name)))
return HttpResponseRedirect(request.path)
else:
self.message_user(request, msg)
return HttpResponseRedirect(reverse("admin:todo_item_changelist"))
admin.site.register(Item, ItemAdmin)
admin.site.register(DateTime, DateAdmin)
生成的错误消息:
name 'User' is not defined
答案 0 :(得分:4)
您可能只需要导入用户:
from django.contrib.auth.models import User