更新:我现在认为这是Django中的一个现有错误,报告为Ticket 14642
这让我疯狂,我认为这是由于我的表单代码,但我意识到现在我可以使用我的模型和管理员重新创建它。我想知道这里的预期行为是什么:
models.py:
class Thingy(models.Model):
description = models.CharField(max_length=256)
class ThingyItem(models.Model):
thingy = models.ForeignKey(Thingy)
description = models.CharField(max_length=256)
admin.py:
class ThingyItemInline(admin.TabularInline):
model = ThingyItem
extra = 0
class ThingyAdmin(admin.ModelAdmin):
inlines = [ThingyItemInline,]
admin.site.register(Thingy, ThingyAdmin)
admin.site.register(ThingyItem)
现在执行以下操作:
当我这样做时,我得到:
Traceback:
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/contrib/admin/options.py" in wrapper
265. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
76. response = view_func(request, *args, **kwargs)
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
78. response = view_func(request, *args, **kwargs)
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/contrib/admin/sites.py" in inner
190. return view(request, *args, **kwargs)
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapper
21. return decorator(bound_func)(*args, **kwargs)
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
76. response = view_func(request, *args, **kwargs)
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/utils/decorators.py" in bound_func
17. return func(self, *args2, **kwargs2)
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/db/transaction.py" in _commit_on_success
299. res = func(*args, **kw)
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/contrib/admin/options.py" in change_view
916. queryset=inline.queryset(request))
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/forms/models.py" in __init__
701. queryset=qs)
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/forms/models.py" in __init__
427. super(BaseModelFormSet, self).__init__(**defaults)
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/forms/formsets.py" in __init__
47. self._construct_forms()
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/forms/formsets.py" in _construct_forms
98. self.forms.append(self._construct_form(i))
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/forms/models.py" in _construct_form
714. form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/forms/models.py" in _construct_form
451. kwargs['instance'] = self.get_queryset()[i]
File "/Users/poswald/.virtualenvs/hats/lib/python2.6/site-packages/django/db/models/query.py" in __getitem__
171. return self._result_cache[k]
Exception Type: IndexError at /admin/exampletest/thingy/1/
Exception Value: list index out of range
我不会在管理员中真正关心这个问题,除非它在我们的生产服务器上使用我自己的表单在代码中发生。似乎内联formset代码非常脆弱。它确实信任使用管理表单发送的数据,而实际应该检查这些假设是否仍然有效。
现在,我认为这值得在Django Trac中报道 - 我现在打算这样做 - 但是我想知道这里是否有人曾经发生这种情况,如果是这样,你是如何解决它的?有没有一种简单的方法来测试表格所假设的这些先决条件是否仍然有效?我希望在我的视图或表单代码中这样做吗?
答案 0 :(得分:3)
对于其他任何人来说,这似乎是Django中的一个错误。我在这里开了一个问题来解决它:http://code.djangoproject.com/ticket/15574