LoginSignup
0
0

More than 3 years have passed since last update.

Django ModelのForeignKey, OneToOneField, ManyToManyFieldのまとめ。

Posted at

1.Foreign KeyとOneToOneFiled

この2つはよく似ている。違いは下のやつ参照。
https://stackoverflow.com/questions/5870537/whats-the-difference-between-django-onetoonefield-and-foreignkey

簡単に言うと、
Aという車(model)があるとする。

ForeignKeyはその車に'所属している'何か(model)に使われる。
ちなみにForeignKeyはOneToManyFieldと呼ばれる。
このForeignKeyは既にある選択肢から選ぶような形をとる。
EX)タイヤ

OneToOneFieldはその車についている'たった一つの'重要な何か(model)に使われる。
EX)エンジン

2.ManyToManyField

これは2つのmodelの関係性で何個でもつながりがあっていい時。
すでにある選択肢からいくつでも選ぶとき。
EX)courseとstudent, tagとproductなど
選択肢となるほうにManyToManyFieldをいれる。EX)course, tag

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0