django was_published_recently 错误

标签 django

教程中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/

相关文章:

django 中间件只有 "process_response"当缺少斜杠时

Django - 带有 FormMixin 和初始值的 DetailView

javascript - 尝试将值从 javascript 发送到 python 服务器

python - Gunicorn,Django,Gevent : Spawned threads are blocking

python - 检查 F 对象的结果是否大于零

python - 表单保存不会将用户添加到模型

python - 如何将 JSON 数据 PUT/POST 到 ListSerializer?

python - 在 Django REST 控件序列化程序中不会自动删除空格?

python - Django-registration - 以异步方式发送电子邮件

html - 将数据从 Django View 作为 JSON 对象传递到 vue 实例