gpt4 book ai didi

python - 为什么我在 Django 中的模型上出现 KeyError 错误?

转载 作者:行者123 更新时间:2023-12-04 13:56:48 49 4
gpt4 key购买 nike

我在线上收到 KeyError: 'conversation' conv = ans.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

最佳答案

KeyError 是 Django 内部预期的,并被处理以获得默认值。
在您的情况下,没有默认值,因此如果在创建时尚未为对话字段提供值时访问该字段,您将看到此 KeyError。

关于python - 为什么我在 Django 中的模型上出现 KeyError 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61000782/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com