【问题标题】:Why do I get a KeyError on a Model in Django?为什么我在 Django 中的模型上出现 KeyError?
【发布时间】:2020-04-02 20:43:03
【问题描述】:

我在代码部分的conv = ans.conversation 行收到一个 KeyError: 'conversation':

answers = Answers.objects.all()

for ans in answers:
    conv = ans.conversation
    ...

我的模型“答案”如下所示:

class Answers(models.Model):
    conversation = models.ForeignKey(Conversation)
    ...

我有时会收到错误,所以如果我运行代码,我会得到几次正确的结果,但最终在 for 循环中,有时conv = ans.conversation 行会失败。您认为为什么会发生这样的错误?

整个错误跟踪在这里:

Traceback (most recent call last):
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 164, in _get_
    rel_obj = self.field.get_cached_value(instance)
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/models/fields/mixins.py", line 13, in get_cached_value
    return instance._state.fields_cache[cache_name]
KeyError: 'conversation'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/webapps/municipio/municipio/reportes/views.py", line 153, in get_whats_data
    conv = ans.conversation
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 178, in _get_
    rel_obj = self.get_object(instance)
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 145, in get_object
    return qs.get(self.field.get_reverse_related_filter(instance))
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/models/query.py", line 402, in get
    num = len(clone)
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/models/query.py", line 256, in _len_
    self._fetch_all()
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/models/query.py", line 55, in _iter_
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
    cursor.execute(sql, params)
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/webapps/municipio/env/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/webapps/municipio/env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 209, in execute
    res = self._query(query)
  File "/webapps/municipio/env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 315, in _query
    db.query(q)
  File "/webapps/municipio/env/lib/python3.6/site-packages/MySQLdb/connections.py", line 226, in query
    _mysql.connection.query(self, query)
  File "/webapps/municipio/env/lib/python3.6/site-packages/gunicorn/workers/base.py", line 196, in handle_abort
    sys.exit(1)
SystemExit: 1

【问题讨论】:

  • 听起来像是系统问题,而不是代码问题。如果将数据库更改为 PosgreSQL 会发生什么?

标签: python django


【解决方案1】:

KeyError 是 Django 内部预期的,并被处理以获得默认值。

在您的情况下,没有默认值,因此如果在创建时未为对话字段提供值时访问该字段,您将看到此 KeyError。

【讨论】:

    猜你喜欢
    • 2021-07-24
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    相关资源
    最近更新 更多