使用Django 1.4根据对象条件更改管理内联

时间:2012-06-22 15:20:01

标签: django django-admin django-1.4

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

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。显然抱怨丢失的内联实例。

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']}>"

我的问题与this one非常类似,甚至提供了解决方案。然而,Django 1.4改变了api,因此不再有ModelAdmin.inline_instances

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

1 个答案:

答案 0 :(得分: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'的内容,但您的代码需要一个。最好检查HTML,InlineModelAdmin和模型,找出POST中没有此类密钥的原因。