Джанго полиморфный администратор «не может создавать экземпляры„NoneType“»

голоса
0

Я пытался сделать основное Джанго полиморфного примера для работы.

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

Кто-нибудь понимает, в чем проблема? Я пробовал все виды изменений. При использовании Джанго по умолчанию администрировать я могу создать проект.

Спасибо,

Лукас

Задан 13/09/2013 в 10:29
источник пользователем
На других языках...                            


1 ответов

голоса
0

Оказалось, что при указании явно форму для использования решить эту проблему:

class ProjectForm(forms.ModelForm):

    class Meta:
        model = Project


class ProjectChildAdmin(PolymorphicChildModelAdmin):
    base_model = Project
    base_form = ProjectForm

По какой-то причине форма не генерируется автоматически.

Ответил 14/09/2013 в 09:23
источник пользователем

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more