我最近将Django更新至2.2.4版,并将python更新至3.7, 我随机遇到此错误:
h:X-My-Arbitrary-Header
我试图调试代码以查找正在发生的情况,看来Traceback (most recent call last):
File "/foo/venv/lib/python3.7/site-packages/django/db/models/query.py", line 73, in __iter__
obj = model_cls.from_db(db, init_list, row[model_fields_start:model_fields_end])
File "/foo/venv/lib/python3.7/site-packages/django/db/models/base.py", line 511, in from_db
for f in cls._meta.concrete_fields
File "/foo/venv/lib/python3.7/site-packages/django/db/models/base.py", line 511, in <listcomp>
for f in cls._meta.concrete_fields
StopIteration
中的from_db
函数正在引起此错误:
django.db.models.base.py
# /foo/venv/lib/python3.7/site-packages/django/db/models/base.py
...
@classmethod
def from_db(cls, db, field_names, values):
if len(values) != len(cls._meta.concrete_fields):
values_iter = iter(values)
values = [
next(values_iter) if f.attname in field_names else DEFERRED
for f in cls._meta.concrete_fields
]
new = cls(*values)
new._state.adding = False
new._state.db = db
return new
...
引发了此错误,似乎Django开发人员应该使用next(values_iter)
块将其包围起来,以使其在python 3.7中起作用,
但是我的问题是,有没有办法解决这个问题,作为临时解决办法?
谢谢。
更新#1:
我已经确切地发现了何时发生此错误。当我在try except
上调用.values_list('myfield', flat=True)
时,查询更改为QuerySet
,这将破坏一切。