【发布时间】: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 会发生什么?