質問するログイン新規登録

回答編集履歴

1

追加

2018/06/02 13:53

投稿

quickquip
quickquip

スコア11353

answer CHANGED
@@ -12,4 +12,28 @@
12
12
  'question': question,
13
13
  'error_message': "You didn't select a choice.",
14
14
  })
15
- ```
15
+ ```
16
+
17
+ ----
18
+
19
+ モデルにauto incrementalなprimary keyがあるなら、それがidにマッピングされています。
20
+
21
+ [https://docs.djangoproject.com/ja/2.0/intro/tutorial02/#activating-models](https://docs.djangoproject.com/ja/2.0/intro/tutorial02/#activating-models)
22
+
23
+
24
+ > あなたが使用しているデータベースに合わせて、auto_increment (MySQL)、 serial (PostgreSQL) もしくは integer primary key autoincrement (SQLite) のようなデータベースに特化した型が自動的に選択され生成されます。フィールド名のクォーティング (例えば、ダブルクォートにするか、シングルクォートにするか) も同様です。
25
+
26
+ とあってこれを読んでいるはずです。
27
+
28
+ そして、
29
+
30
+ [https://docs.djangoproject.com/ja/2.0/intro/tutorial02/#playing-with-the-api](https://docs.djangoproject.com/ja/2.0/intro/tutorial02/#playing-with-the-api)
31
+
32
+ ここで、Questionクラスの定義にid属性がないにも関わらず`q.id`が引けていることも、`q.choice_set.all()`でChoiceのリストを取り出せることを**確認しているはず**です。
33
+
34
+ Choiceもauto incrementalなprimary keyを持ってますから。`id`があります。
35
+
36
+ ----
37
+
38
+ [https://docs.djangoproject.com/en/2.0/ref/models/instances/#auto-incrementing-primary-keys](https://docs.djangoproject.com/en/2.0/ref/models/instances/#auto-incrementing-primary-keys)
39
+ このあたりですね。