gpt4 book ai didi

python - FieldError : Invalid field name(s) given in select_related: 'userinfo' . 选择是:用户信息

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:31 24 4
gpt4 key购买 nike

尝试将 onlyselect_related 一起使用时出现此错误。

FieldError: Invalid field name(s) given in select_related: 'userinfo'. Choices are: userinfo

有点奇怪,它报告我正在尝试选择的字段为错误。这是我的查询:

users_with_schools = User.objects.select_related('userinfo').only(
"id",
"date_joined",
"userinfo__last_coordinates_id",
"userinfo__school_id"
).filter(
userinfo__school_id__isnull=False,
date_joined__gte=start_date
)

我已经能够在代码的其他地方将 select_relatedonly 一起使用,所以我不确定为什么会这样。

编辑:这是完整的回溯

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "env/lib/python2.7/site-packages/django/db/models/query.py", line 138, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "env/lib/python2.7/site-packages/django/db/models/query.py", line 162, in __iter__
self._fetch_all()
File "env/lib/python2.7/site-packages/django/db/models/query.py", line 965, in _fetch_all
self._result_cache = list(self.iterator())
File "env/lib/python2.7/site-packages/django/db/models/query.py", line 238, in iterator
results = compiler.execute_sql()
File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
sql, params = self.as_sql()
File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 367, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 48, in pre_sql_setup
self.setup_query()
File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 39, in setup_query
self.select, self.klass_info, self.annotation_col_map = self.get_select()
File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 203, in get_select
related_klass_infos = self.get_related_selections(select)
File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 743, in get_related_selections
', '.join(_get_field_choices()) or '(none)',
FieldError: Invalid field name(s) given in select_related: 'userinfo'. Choices are: userinfo

最佳答案

来自documentation :

All of the cautions in the note for the defer() documentation apply to only() as well. Use it cautiously and only after exhausting your other options.

...

Using only() and omitting a field requested using select_related() is an error as well.

select_related将尝试获取 userinfo所有列.如上所述,尝试将列集限制为特定列将导致错误 - select_related 的组合和 only不支持。

可能值得注意的是这些方法的注释:

The defer() method (and its cousin, only(), below) are only for advanced use-cases. They provide an optimization for when you have analyzed your queries closely and understand exactly what information you need and have measured that the difference between returning the fields you need and the full set of fields for the model will be significant.

编辑:您在下面的评论中提到,在您代码的其他地方使用的以下查询似乎工作正常:

ChatUser.objects.select_related("user__userinfo").\
only( "id", "chat_id", "user__id", "user__username", "user__userinfo__id" )

我最好的猜测是你正在点击 this bug在 Django ( fixed in 1.10 ) 中。

我想最简单的验证方法是检查似乎有效的查询集生成的 SQL 查询。我的猜测是您会发现它实际上并没有一次查询所有内容,并且当您尝试访问您要求在 select_related 中获取的相关模型时还有其他查询。 .

关于python - FieldError : Invalid field name(s) given in select_related: 'userinfo' . 选择是:用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37261490/

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