python – FieldError:select_related:’userinfo’中给出的无效字段名称.选择是:userinfo
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – FieldError:select_related:’userinfo’中给出的无效字段名称.选择是:userinfo,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3206字,纯文字阅读大概需要5分钟。
内容图文
![python – FieldError:select_related:’userinfo’中给出的无效字段名称.选择是:userinfo](/upload/InfoBanner/zyjiaocheng/727/dcd29c77fc5f46a1b1b9fdf415c52ca7.jpg)
尝试仅使用select_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_related,所以我不确定为什么会发生这种情况.
编辑:这是完整的追溯
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
解决方法:
All of the cautions in the note for the
defer()
documentation apply toonly()
as well. Use it cautiously and only after exhausting your other options.…
Using
only()
and omitting a field requested usingselect_related()
is an error as well.
select_related将尝试获取userinfo的所有列.如上所述,尝试将列集限制为特定列将导致错误 – select_related的组合并且仅支持该错误.
值得注意的是这些方法的评论:
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" )
我最好的猜测是你在Django(fixed in 1.10)击中了this bug.
我想最简单的验证方法是检查查询集生成的SQL查询是否有效.我的猜测是,您会发现它实际上并不是一次性查询所有内容,并且当您尝试访问要求在select_related中获取的相关模型时还有其他查询.
内容总结
以上是互联网集市为您收集整理的python – FieldError:select_related:’userinfo’中给出的无效字段名称.选择是:userinfo全部内容,希望文章能够帮你解决python – FieldError:select_related:’userinfo’中给出的无效字段名称.选择是:userinfo所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。