我一直试图让基本的django-polymorphic例子起作用。
models.py
from polymorphic import PolymorphicModel, ShowFieldContent
class Project(ShowFieldContent, PolymorphicModel):
topic = models.CharField(max_length=30)
class ArtProject(Project):
artist = models.CharField(max_length=30)
class ResearchProject(Project):
supervisor = models.CharField(max_length=30)
admin.py
from django.contrib import admin
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin, PolymorphicChildModelFilter
from rapidhealth.core.models import Project, ArtProject, ResearchProject
class ProjectChildAdmin(PolymorphicChildModelAdmin):
base_model = Project
class ProjectAdmin(PolymorphicParentModelAdmin):
base_model = Project
list_filter = (PolymorphicChildModelFilter,)
child_models = (
(Project, ProjectChildAdmin),
(ArtProject, ProjectChildAdmin),
(ResearchProject, ProjectChildAdmin),
)
admin.site.register(Project, ProjectAdmin)
现在,当我尝试在管理网站中创建一个新的“项目”时,我得到了:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/admincore/project/add/?ct_id=42
Django Version: 1.5.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'sekizai',
'mptt',
'south',
'reversion',
'rest_framework',
'polymorphic',
'rapidhealth.core',
'debug_toolbar')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware')
Traceback:
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
372. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
202. return view(request, *args, **kwargs)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/polymorphic/admin.py" in add_view
225. return real_admin.add_view(request, form_url, extra_context)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
223. return func(*args, **kwargs)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
984. ModelForm = self.get_form(request)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/polymorphic/admin.py" in get_form
484. return super(PolymorphicChildModelAdmin, self).get_form(request, obj, **kwargs)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in get_form
465. return modelform_factory(self.model, **defaults)
File "/home/lucas-fievet/.virtualenvs/rh/local/lib/python2.7/site-packages/django/forms/models.py" in modelform_factory
424. return type(form)(class_name, (form,), form_class_attrs)
Exception Type: TypeError at /admincore/project/add/
Exception Value: cannot create 'NoneType' instances
有谁知道问题是什么? 我尝试过各种改变。使用默认的django admin时,我可以创建一个项目。
谢谢,
卢卡斯
答案 0 :(得分:0)
事实证明明确指定要使用的表单解决了问题:
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
class ProjectChildAdmin(PolymorphicChildModelAdmin):
base_model = Project
base_form = ProjectForm
由于某种原因,表格不会自动生成。