0

ModelAdmin我的应用程序中,我想change_view根据对象的特定条件更改 a 的内联。

class TextInline(generic.GenericStackedInline):
    model = Text

class ImageInline(generic.GenericStackedInline):
    model = Image

class VideoInline(generic.GenericStackedInline):
    model = Video

class PageAdmin(models.ModelAdmin):
    def add_view(self, request, form_url='', extra_context=None):
        self.inlines = []
        return super(PageAdmin, self).add_view(
            request, form_url, extra_context)

    def change_view(self, request, object_id, form_url='', extra_context=None):
        obj = Page.objects.get(id=object_id)

        if obj.page_type in ('foo', 'bar',):
            self.inlines = [TextInline]
        elif obj.page_type == 'baz':
            self.inlines = [ImageInline, VideoInline]

        return super(PageAdmin, self).change_view(
            request, object_id, form_url, extra_context)

add_view按预期工作,不显示内联。change_view还会在初始加载时显示正确的内联,并在第一次保存后显示正确的内联实例。当再次保存页面时,MultiValueDictKeyError会引发 an。显然抱怨缺少内联实例。

Traceback:
File "/path/to/django/core/handlers/base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/path/to/django/contrib/admin/options.py" in wrapper
366.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/path/to/django/utils/decorators.py" in _wrapped_view
91.                     response = view_func(request, *args, **kwargs)
File "/path/to/django/views/decorators/cache.py" in _wrapped_view_func
89.         response = view_func(request, *args, **kwargs)
File "/path/to/django/contrib/admin/sites.py" in inner
196.             return view(request, *args, **kwargs)
File "/path/to/presentation/admin.py" in change_view
151.             request, object_id, form_url, extra_context)
File "/path/to/django/utils/decorators.py" in _wrapper
25.             return bound_func(*args, **kwargs)
File "/path/to/django/utils/decorators.py" in _wrapped_view
91.                     response = view_func(request, *args, **kwargs)
File "/path/to/django/utils/decorators.py" in bound_func
21.                 return func(self, *args2, **kwargs2)
File "/path/to/django/db/transaction.py" in inner
209.                 return func(*args, **kwargs)
File "/path/to/django/contrib/admin/options.py" in change_view
1049.                                   queryset=inline.queryset(request))
File "/path/to/django/contrib/contenttypes/generic.py" in __init__
402.             prefix=prefix
File "/path/to/django/forms/models.py" in __init__
424.         super(BaseModelFormSet, self).__init__(**defaults)
File "/path/to/django/forms/formsets.py" in __init__
50.         self._construct_forms()
File "/path/to/django/forms/formsets.py" in _construct_forms
115.             self.forms.append(self._construct_form(i))
File "/path/to/django/forms/models.py" in _construct_form
443.             pk = self.data[pk_key]
File "/path/to/django/utils/datastructures.py" in __getitem__
258.             raise MultiValueDictKeyError("Key %r not found in %r" % (key, self))

Exception Type: MultiValueDictKeyError at /admin/presentation/page/5/
Exception Value: "Key 'presentation-text-content_type-object_id-0-id' not found in <QueryDict: {u'status': [u'2'], u'presentation-text-content_type-object_id-1-text': [u'xxx'], u'title': [u'y'], u'presentation-text-content_type-object_id-0-text': [u'xx'], u'presentation-text-content_type-object_id-__prefix__-ordering': [u''], u'presentation-text-content_type-object_id-TOTAL_FORMS': [u'2'], u'presentation-text-content_type-object_id-0-ordering': [u''], u'presentation-text-content_type-object_id-1-ordering': [u''], u'presentation-text-content_type-object_id-__prefix__-text': [u''], u'presentation-text-content_type-object_id-MAX_NUM_FORMS': [u''], u'csrfmiddlewaretoken': [u'6f53e0394b9008494a53cf460826235c'], u'presentation': [u'1'], u'_continue': [u'Sichern und weiter bearbeiten'], u'presentation-text-content_type-object_id-INITIAL_FORMS': [u'1']}>"

我的问题与这个甚至提供解决方案的问题非常相似。然而,Django 1.4 更改了 api,因此ModelAdmin.inline_instances不再存在。

在 Django 1.4 中,您可以使用它ModelAdmin.get_inline_instances()来检索内联实例列表,但我不知道如何设置它们。或者在 Django 1.4 中有另一种方法吗?api更改可能是有原因的。

4

1 回答 1

2

错误发生在

File "/path/to/django/contrib/admin/options.py" in change_view
1049.                                   queryset=inline.queryset(request))

那里inline_instances已经成功生成。

通过检查您的POST

>>> post = {u'status': [u'2'], u'presentation-text-content_type-object_id-1-text': [u'xxx'], u'title': [u'y'], u'presentation-text-content_type-object_id-0-text': [u'xx'], u'presentation-text-content_type-object_id-__prefix__-ordering': [u''], u'presentation-text-content_type-object_id-TOTAL_FORMS': [u'2'], u'presentation-text-content_type-object_id-0-ordering': [u''], u'presentation-text-content_type-object_id-1-ordering': [u''], u'presentation-text-content_type-object_id-__prefix__-text': [u''], u'presentation-text-content_type-object_id-MAX_NUM_FORMS': [u''], u'csrfmiddlewaretoken': [u'6f53e0394b9008494a53cf460826235c'], u'presentation': [u'1'], u'_continue': [u'Sichern und weiter bearbeiten'], u'presentation-text-content_type-object_id-INITIAL_FORMS': [u'1']}
>>> sorted(post.keys())
[u'_continue',
 u'csrfmiddlewaretoken',
 u'presentation',
 u'presentation-text-content_type-object_id-0-ordering',
 u'presentation-text-content_type-object_id-0-text',
 u'presentation-text-content_type-object_id-1-ordering',
 u'presentation-text-content_type-object_id-1-text',
 u'presentation-text-content_type-object_id-INITIAL_FORMS',
 u'presentation-text-content_type-object_id-MAX_NUM_FORMS',
 u'presentation-text-content_type-object_id-TOTAL_FORMS',
 u'presentation-text-content_type-object_id-__prefix__-ordering',
 u'presentation-text-content_type-object_id-__prefix__-text',
 u'status',
 u'title']

没有类似的东西,u'presentation-text-content_type-object_id-__prefix__-id'但您的代码需要一个。所以最好检查 HTMLInlineModelAdmin和模型以找出为什么POST.

于 2012-06-22T16:31:20.600 回答