Django中的“元组索引超出范围”错误

6yt4nkrj  于 2023-04-13  发布在  Go
关注(0)|答案(1)|浏览(160)

不久前,我使用了一个常规的单元测试(不是django测试),现在我的项目中有一些保存的测试数据。我可以在管理面板中打开它们的页面,但当试图删除它们时,收到错误“tuple index out of range”。我该如何解决这个问题?
对于透明病例:url**/admin/quest_manager/quest/15/change/正在工作,url/admin/quest_manager/quest/15/delete/**引发上述错误。
以前和现在,我从来没有在视图、序列化器和模型中使用位置参数。
我使用PostgreSQL作为数据库,如果这很重要的话。
在基地干扰项目不必要的数据,我想删除这个元素,而不完全清除数据库中的表。
感谢您的帮助!

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/admin/quest_manager/quest/15/delete/

Django Version: 4.1.7
Python Version: 3.10.6
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'debug_toolbar',
 'social_django',
 'quest_manager']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'debug_toolbar_force.middleware.ForceDebugToolbarMiddleware']


Traceback (most recent call last):
  File "/home/horhe/.local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 56, in inner
    response = get_response(request)
  File "/home/horhe/.local/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/horhe/.local/lib/python3.10/site-packages/django/contrib/admin/options.py", line 686, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/home/horhe/.local/lib/python3.10/site-packages/django/utils/decorators.py", line 134, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/horhe/.local/lib/python3.10/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/horhe/.local/lib/python3.10/site-packages/django/contrib/admin/sites.py", line 242, in inner
    return view(request, *args, **kwargs)
  File "/home/horhe/.local/lib/python3.10/site-packages/django/utils/decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
  File "/home/horhe/.local/lib/python3.10/site-packages/django/utils/decorators.py", line 134, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/horhe/.local/lib/python3.10/site-packages/django/contrib/admin/options.py", line 2110, in delete_view
    return self._delete_view(request, object_id, extra_context)
  File "/home/horhe/.local/lib/python3.10/site-packages/django/contrib/admin/options.py", line 2138, in _delete_view
    ) = self.get_deleted_objects([obj], request)
  File "/home/horhe/.local/lib/python3.10/site-packages/django/contrib/admin/options.py", line 2105, in get_deleted_objects
    return get_deleted_objects(objs, request, self.admin_site)
  File "/home/horhe/.local/lib/python3.10/site-packages/django/contrib/admin/utils.py", line 153, in get_deleted_objects
    to_delete = collector.nested(format_callback)
  File "/home/horhe/.local/lib/python3.10/site-packages/django/contrib/admin/utils.py", line 220, in nested
    roots.extend(self._nested(root, seen, format_callback))
  File "/home/horhe/.local/lib/python3.10/site-packages/django/contrib/admin/utils.py", line 204, in _nested
    children.extend(self._nested(child, seen, format_callback))
  File "/home/horhe/.local/lib/python3.10/site-packages/django/contrib/admin/utils.py", line 206, in _nested
    ret = [format_callback(obj)]
  File "/home/horhe/.local/lib/python3.10/site-packages/django/contrib/admin/utils.py", line 128, in format_callback
    no_edit_link = "%s: %s" % (capfirst(opts.verbose_name), obj)
  File "/home/horhe/PycharmProjects/tabor_project/quest_manager/models.py", line 31, in __str__
    str_status = self.QUEST_STATUS[self.status][1]

Exception Type: IndexError at /admin/quest_manager/quest/15/delete/
Exception Value: tuple index out of range
dgtucam1

dgtucam11#

我已经理解了这个问题。一些对象没有一个字段,它在后面添加了,Django尝试在与索引的关系中接收空字段,即使关系不存在。

相关问题