0

我正在尝试使用 Django 进行民意调查,但我对它很陌生,当我进入管理页面时,它会出现以下错误:

ImproperlyConfigured at /admin/polls/poll/1/
'PollAdmin.fields' refers to field 'None' that is missing from the form.

我不太确定这意味着什么,但这是我认为导致错误的 admin.py 文件:

from django.contrib import admin
from polls.models import Poll

class PollAdmin(admin.ModelAdmin):
  fields = [
    (None,         {'fields': ['question']}),
    ('Date information', {'fields': ['pub date']}),

admin.site.register(Poll, PollAdmin)

编辑

如果这很重要,这是我的模型文件:

from django.db import models
import datetime
from django.utils import timezone

class Poll(models.Model):
  question = models.CharField(max_length = 200)
  pub_date = models.DateTimeField('date published')
  def __unicode__(self):
    return self.question
  def was_published_recently(self):
    return self.pub_date >= timezone.now() - datetime.timedelta(days = 1)

class Choice(models.Model):
  poll = models.ForeignKey(Poll)
  choice_text = models.CharField(max_length = 200)
  votes = models.IntegerField()
  def __unicode__(self):
    return self.choice_text

编辑(再次)

现在我将我的 PollAdmin 类修复为:

class PollAdmin(admin.ModelAdmin):
  fieldsets = [
    (None,         {'fields': ['question']}),
    ('Date information', {'fields': ['pub_date']}),
  ]

但它说:

DoesNotExist at /admin/polls/poll/1/
Site matching query does not exist.
4

1 回答 1

4

使用字段集而不是字段:

class PollAdmin(admin.ModelAdmin):
  fieldsets = [
    (None,         {'fields': ['question']}),
    ('Date information', {'fields': ['pub_date']}),
于 2012-08-01T00:15:47.457 回答