0
0

More than 1 year has passed since last update.

django で has_many のデータ取得 ( Rails と比較 )

Posted at

たとえばArticleが複数のCommentを持つ場合

Rails の場合

article.comments

djangoの場合

article.comment_set.all()

モデル定義の例

Railsだと

  • 親クラスに has_many 関連を書く
  • 子クラスに belongs_to 関連を書く

というやり方だが、 djangoだと子クラスにだけ関連を記述すれば良いようだ

class Article(models.Model):
    ...

class Comment(models.Model):
    article = models.ForeignKey(Article, on_delete=models.CASCADE)

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

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