Я получаю KeyError: 'разговор' на линии conv = ans.conversation
в разделе кода:
answers = Answers.objects.all()
for ans in answers:
conv = ans.conversation
...
Моя модель 'Ответы' выглядит так:
class Answers(models.Model):
conversation = models.ForeignKey(Conversation)
...
Иногда я получаю сообщение об ошибке, поэтому, если я запускаю код, я получаю правильный результат несколько раз, но в конце концов в течение l oop иногда строка 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