编程语言
首页 > 编程语言> > python – FieldError:select_related:’userinfo’中给出的无效字段名称.选择是:userinfo

python – FieldError:select_related:’userinfo’中给出的无效字段名称.选择是:userinfo

作者:互联网

尝试仅使用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

解决方法:

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的组合并且仅支持该错误.

值得注意的是这些方法的评论:

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,django,django-queryset
来源: https://codeday.me/bug/20190829/1758315.html