0
0

More than 1 year has passed since last update.

Django(4.1~)でbulk create or updateを実現する

Posted at

概要(忙しい人はここだけ読んで公式docを参照してください)

  • 目的
    • update_or_create()(DBに存在しなければ新規作成、存在すれば更新)の処理をより高速に行うため、bulk_update_or_create()的なことを実現したい。
  • 課題
    • Djangoではbulk_update_or_create()というメソッドは提供されていない。
    • ネットで検索しても、自前で関数を用意する方法しか出てこない。
  • 結論
    • Django4.1以降であれば、bulk_create()のパラメータでupdate_conflicts=Trueを指定することで実現可能。
      • unique_fieldsでunique constraintをかけるフィールドを指定(リスト)
      • update_fieldsで更新する場合の対象フィールドを指定(リスト)
    • https://docs.djangoproject.com/en/4.2/ref/models/querysets/#bulk-create

やりたかったこと

(執筆中…)

実現方法

(執筆中…)

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