0

我正在关注“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'>

我的代码几乎相同(除了我包含导入语句):

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)

我查看了类似的 SO 线程,但我无法从那里弄清楚这里出了什么问题。(顺便说一句,我在教程中使用了django 1.3while django 1.2

4

2 回答 2

3

您应该在模型中替换created = models.DateTimeField(auto_now_add = True)为。created = models.ForeignKey(DateTime)Item

于 2012-07-19T15:53:13.283 回答
3

您的模型与教程中的模型不匹配。你Item.createdDateTimeField他们的Item.created一个模型。ForeignKeyDateTime

# 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)
于 2012-07-19T15:53:51.123 回答