无法将模型对象映射到管理员

Could not map model object to admin

我正在尝试将模型对象添加到管理面板,但我正在 "TypeError: 'DeclarativeFieldsMetaclass' object is not iterable"

这是models.py

from django.db import models
from django import forms
from django.forms.widgets import *
from django.core.mail import send_mail, BadHeaderError

class ContactForm(forms.Form):
    email = forms.EmailField(required=True)
    name = forms.CharField(required=True)
    topic = forms.CharField()
    message = forms.CharField(widget=forms.Textarea)

    def __unicode__(self):
        return "%s" %(self.email)

admin.py:

from django.contrib import admin

from .models import ContactForm

class ContactFormAdmin(admin.ModelAdmin):
    list_display = []
    class Meta:
        model = ContactForm


admin.site.register(ContactForm)

views.py:

from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from query_form.models import ContactForm
from django.template import RequestContext, Context
from django import forms
from django.forms.widgets import *
from django.core.mail import send_mail, BadHeaderError

def contactview(request):
        subject = request.POST.get('topic', '')
        message = request.POST.get('message', '')
        from_email = request.POST.get('email', '')

        if subject and message and from_email:
                try:
                    send_mail(subject, message, from_email, ['localhost'])
                except BadHeaderError:
                        return HttpResponse('Invalid header found.')
                return HttpResponseRedirect('/thankyou/')
        else:
            return render_to_response('home.html', {'form': ContactForm()}, RequestContext(request))

        return render_to_response('home.html', {'form': ContactForm()},
            RequestContext(request))

def thankyou(request):
        return render_to_response('thankyou.html', context_instance=RequestContext(request))

模型、视图、URL 工作正常,但使用上述 admin.py 代码时出现以下错误:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vicodin/.virtualenvs/contact/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/vicodin/.virtualenvs/contact/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/home/vicodin/.virtualenvs/contact/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/vicodin/.virtualenvs/contact/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/home/vicodin/.virtualenvs/contact/local/lib/python2.7/site-packages/django/contrib/admin/apps.py", line 22, in ready
    self.module.autodiscover()
  File "/home/vicodin/.virtualenvs/contact/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 23, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/home/vicodin/.virtualenvs/contact/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/vicodin/Dropbox/bunty/work/django/lappymate/query_form/admin.py", line 11, in <module>
    admin.site.register(ContactForm)
  File "/home/vicodin/.virtualenvs/contact/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 80, in register
    for model in model_or_iterable:
TypeError: 'DeclarativeFieldsMetaclass' object is not iterable

编辑: 我已经尝试通过将 admin.py 更新为

来回答@catavaran
from django.contrib import admin
from .models import ContactForm

class ContactFormAdmin(admin.ModelAdmin):
    pass

admin.site.register(ContactForm, ContactFormAdmin)

但仍然出现相同的错误。

您的模型应该继承自 models.Model 而不是 forms.Form:

class ContactForm(models.Model):
    ...

你的 ModelAdmin 和注册应该是这样的:

class ContactFormAdmin(admin.ModelAdmin):
    list_display = ('name', 'email', 'topic', )

admin.site.register(ContactForm, ContactFormAdmin)

Metamodel 是 django 形式的属性,而不是 ModelAdmin