2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DjangoでModelをDictionaryに一瞬で変換し、爆速でFormを初期化する

Last updated at Posted at 2020-08-15

概要

諸事情がありModelFormを使わないこととしたが、FormをModelで初期化する際に、コンストラクタの引数にDictonary型で与えるのが地味に面倒だったので調べたときのメモ

やりかた

Modelオブジェクトの__dict__メソッドを呼び出すだけ。

model = Compound.objects.get(id=compound_id)
form = CompoundForm(compound.__dict__)

参考文献には他にも様々なやり方が記載されている。

参考文献

Convert Django Model object to dict with all of the fields intact

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?