2016-05-16 19 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 
) 

私はなぜこれが起こっているので、私はわからない私のコードでは他の場所でonlyselect_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 
+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_relateduserinfoためすべて列を取得しようとします。上で説明したように、列のセットを特定の列に限定しようとすると、エラーが発生します。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をDjango(fixed 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__id''を '' user__userinfo__school_id''に置き換えてみて、同様の例外が発生するかどうか確認してください。 –

+0

@EricConner私は、この矛盾した振る舞いの可能な説明で私の答えを編集しました。 – solarissmoke