【问题标题】:FieldError: Invalid field name(s) given in select_related: 'userinfo'. Choices are: userinfoFieldError:select_related 中给出的字段名称无效:'userinfo'。选项有:用户信息
【发布时间】:2016-05-16 19:12:51
【问题描述】:

我在尝试将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

【问题讨论】:

  • 您介意在您的问题中包含您的模型吗?
  • 能否请您发布模型,尤其是目标 FK userinfo

标签: python django django-queryset


【解决方案1】:

来自documentation

defer() 文档注释中的所有注意事项也适用于only()。请谨慎使用,仅在用尽其他选项后才可使用。

...

使用only() 并省略使用select_related() 请求的字段也是错误的。

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

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

defer() 方法(及其表亲,only(),如下)仅适用于高级用例。当您仔细分析查询并准确了解您需要哪些信息并衡量返回您需要的字段与模型的完整字段集之间的差异将是显着时,它们会提供优化。

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

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 中获取的相关模型时,还会有其他查询。

【讨论】:

  • 感谢您的回答。在另一个地方我有:chatusers_with_info = ChatUser.objects.select_related("user__userinfo").only("id", "chat_id", "user__id", "user__username", "user__userinfo__id") 这工作正常。所以我的问题是为什么一个有效而另一个无效,因为它们对我来说似乎是一样的。
  • 这可能与不能延迟主键的事实有关。无论如何,您报告的案例中的错误消息应该得到改进。您可以尝试将'user__userinfo__id' 替换为'user__userinfo__school_id',看看是否有类似的异常?
  • @EricConner 我已经编辑了我的答案,并为这种矛盾的行为提供了可能的解释。
猜你喜欢
  • 1970-01-01
  • 2016-03-06
  • 1970-01-01
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 2021-01-13
相关资源
最近更新 更多