我正在关注'{3}}部分'django by example'教程。现在,当我转到/admin/todo/datetime/
并点击Add date time
时,我收到此错误:
Exception Type: Exception at /admin/todo/datetime/add/
Exception Value: <class 'todo.models.Item'> has no ForeignKey to <class 'todo.models.DateTime'>
我的代码完全相同(除了我包含了import语句):
from django.db import models
from django.contrib import admin
class DateTime(models.Model):
datetime = models.DateTimeField(auto_now_add = True)
def __unicode__(self):
return unicode(self.datetime)
class Item(models.Model):
name = models.CharField(max_length = 60)
created = models.DateTimeField(auto_now_add = True)
priority = models.IntegerField(default = 0)
difficulty = models.IntegerField(default = 0)
done = models.BooleanField(default = False)
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]
admin.site.register(Item, ItemAdmin)
admin.site.register(DateTime, DateAdmin)
我看着类似的S.O.线程,但我无法弄清楚这里出了什么问题。
(顺便说一下,我使用django 1.3
而django 1.2
用于turorial)
答案 0 :(得分:3)
您应该将created = models.DateTimeField(auto_now_add = True)
替换为created = models.ForeignKey(DateTime)
模型中的Item
。
答案 1 :(得分:3)
您的模型与教程中的模型不匹配。您的Item.created
为DateTimeField
,Item.created
为ForeignKey
模型的DateTime
。
# From http://lightbird.net/dbe/todo_list.html#adding-multiple-items
class DateTime(models.Model):
datetime = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return unicode(self.datetime)
class Item(models.Model):
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)