教程中here我回到你运行 was_published_recently 的地方,我得到了这个错误:
ImproperlyConfigured at /admin/polls/poll/ PollAdmin.list_display[2], 'was_published_recently' is not a callable or an attribute of 'PollAdmin' or found in the model 'Poll'. Request Method: GET Request URL: /admin/polls/poll/ Django Version: 1.4 Exception Type: ImproperlyConfigured Exception Value:
PollAdmin.list_display[2], 'was_published_recently' is not a callable or an attribute of 'PollAdmin' or found in the model 'Poll'. Exception Location: C:\Python27\lib\site-packages\django\contrib\admin\validation.py in validate, line 38
这是我的代码:
from polls.models import Poll
from django.contrib import admin
from polls.models import Choice
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 3
class PollAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
class PollAdmin(admin.ModelAdmin):
# ...
list_display = ('question', 'pub_date', 'was_published_recently')
admin.site.register(Poll, PollAdmin)
这是我的投票模型
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
class Poll(models.Model):
# ...
def __unicode__(self):
return self.question
class Choice(models.Model):
# ...
def __unicode__(self):
return self.choice
最佳答案
你能用你的 Poll
模型更新你的问题吗?
看起来您在将 was_published_recently
方法添加到 Playing with the API step 中的 Poll
模型时可能犯了一个错误在教程 1 中。
更新:
既然您已经发布了您的模型,看起来您确实错过了 was_published_recently
方法。返回教程 1 并将其添加进去。
其次,不要在 models.py 中多次包含每个模型 - 第二个将替换第一个。
关于django was_published_recently 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11412222/