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

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

尝试将 onlyselect_related 结合使用时出现此错误。

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

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

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_relatedonly 的组合不支持。

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

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 in Django (fixed in 1.10)。

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

中获取的相关模型时还有其他查询