2016-05-16 40 views
3

當試圖使用onlyselect_related時出現此錯誤。FieldError:在select_related:'userinfo'中給出的無效字段名稱。選擇是:userinfo

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 
+0

你介意在你的問題中包括你的模型嗎? –

+0

您可以發佈模型,特別是目標FK'userinfo'嗎? – trinchet

回答

5

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") 

我最好的猜測是你在Django中打this bugfixed in 1.10)。

我想最簡單的驗證方法是檢查查詢集生成的SQL查詢似乎工作。我的猜測是,你會發現它並不是實際查詢所有內容,並且當你嘗試訪問你要求在select_related中提取的相關模型時還有其他查詢。

+0

感謝您的回答。在另一個地方,我有: chatusers_with_info = ChatUser.objects.select_related( 「user__userinfo」)只( 「ID」, 「chat_id」, 「user__id」, 「user__username」, 「user__userinfo__id」 ) 。這工作正常。所以我的問題是爲什麼一個人工作,而另一個不是,因爲他們似乎對我來說是一樣的。 –

+0

它可能與事實主鍵不能推遲。無論如何,你的報告中的錯誤信息應該得到改善。您可以嘗試用''user__userinfo__school_id''替換''user__userinfo__id''並查看是否有類似的異常? –

+0

@EricConner我已經編輯了我的答案,並解釋了這種矛盾的行爲。 – solarissmoke

相關問題