I get this error when trying to use only
with select_related
.
FieldError: Invalid field name(s) given in select_related: 'userinfo'. Choices are: userinfo
It's a little strange that it reports the field I'm trying to select as an error. Here is my query:
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
)
I've been able to use select_related
with only
in other places in my code so I am not sure why this is happening.
Edit: Here is the full traceback
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
From the documentation:
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
will try to fetch all the columns for userinfo
. As described above, trying to restrict the set of columns to specific ones will result in an error - the combination of select_related
and only
does not support that.
It's probably worth noting the comment that goes with these methods:
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.
Edit: you mention in a comment below that the following query, used elsewhere in your code, seems to work fine:
ChatUser.objects.select_related("user__userinfo").\
only( "id", "chat_id", "user__id", "user__username", "user__userinfo__id" )
My best guess is that you are hitting this bug in Django (fixed in 1.10).
I suppose the easiest way to verify is to check the SQL query generated by the queryset that seems to work. My guess is that you will find that it isn't actually querying everything in one go and that there are additional queries when you try to access the related model that you asked to be fetched in select_related
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With