... ields = [ "name" , "authors" ] これらのモデルでは、上記の ModelForm サブクラスはおおむね以下と同等と言えます (唯一の違いは save() メソッドで、これからすぐ説明します): from django import forms class AuthorForm ( forms . Form ): name = forms . CharFiel ... : error_messages = { NON_FIELD_ERRORS : { "unique_together" : " %(model_name)s 's %(field_labels)s are not unique." , } } save() メソッド 全ての ModelForm は save() メソッドも持っています。このメソッドは、フォームに結びつけられたデータからデータベー ... delForm のサブクラスはキーワード引数 instance として既存のモデルインスタンスを受け取ることができます; これが指定されると、 save() はそのインスタンスを更新するようになります。 指定されない場合は、 save() は指定されたモデルの新しいインスタンスを生成しま ... ... ields you've defined on your model. Note that instantiating a model in no way touches your database; for that, you need to save() . 注釈 You may be tempted to customize the model by overriding the __init__ method. If you do so, however, take care not ... to change the calling signature as any change may prevent the model instance from being saved. Additionally, referring to model fields within __init__ may potentially result in infinite recursion errors in some circ ... though a field assigned a value of a different type (for example, a string assigned to a UUIDField ) may be converted when save() executes a query, the in-memory instance will keep the original value until it is reloaded . モデルの読み込みをカスタマイ ... ... ソッド オブジェクト(以下の Car.photo のように)に関連付けられた File には、いくつかの追加メソッドも用意されています。 File. save ( name , content , save = True ) 提供されたファイル名と内容で新しいファイルを保存します。これは既存のファイルを置き換えるも ... のではありませんが、新しいファイルを作成し、オブジェクトがそれを指すように更新します。 save が True の場合、ファイルが保存されたらモデルの save() メソッドが一度呼び出されます。つまり、次の二行です: >>> car . photo ... . save ( "myphoto.jpg" , content , save = False ) >>> car . save () これは以下と同等です: >>> car . photo . save ( "myphoto.jpg" ... ... object instance that is related to the read or write operation that is underway. This might be the instance that is being saved, or it might be an instance that is being added in a many-to-many relation. In some cases, no instance hint will be provi ... he 'auth_db' database >>> fred = User . objects . get ( username = "fred" ) >>> fred . first_name = "Frederick" >>> # This save will also be directed to 'auth_db' >>> fred . save () >>> # These retrieval will be randomly allocated to a replica databa ... ent will consult the router, and set mh onto >>> # the same database as the author object >>> mh . author = dna >>> # This save will force the 'mh' instance onto the primary database... >>> mh . save () >>> # ... but if we re-retrieve the object, it ... ... ルする Django は、データベーストランザクションをコントロールするための一つの API を提供しています。 atomic ( using = None , savepoint = True , durable = False ) [ソース] Atomicityは、データベーストランザクションの定義プロパティです。 atomic は、データ ... DatabaseError , transaction obj = MyModel ( active = False ) obj . active = True try : with transaction . atomic (): obj . save () except DatabaseError : obj . active = False if obj . active : ... これはキャッシュやグローバル変数など、アプリの状態を保 ... を解放するかロールバックします; 一番外側のブロックを抜けるときに、トランザクションをコミットするかロールバック します。 引数 savepoint を False に設定することで、内部ブロックのセーブポイン トの作成を無効にできます。例外が発生した場合、 Django はセーブポ ... ... 新を行います。これにはオブジェクトが既に保存されている必要があります。 引数 bulk=False を使用することで、関係マネージャが e.save() を呼び出して更新を行うことができます。 しかし、多対多のリレーションシップで add() を使用すると、 save() メソッドは呼び出 ... create ( ... headline = "Hello" , body_text = "Hi" , pub_date = datetime . date ( 2005 , 1 , 1 ) ... ) # No need to call e.save() at this point -- it's already been saved. これは下記のコードと等価です(しかし、よりシンプルです): >>> b = Blog . objec ... ) >>> e = Entry ( blog = b , headline = "Hello" , body_text = "Hi" , pub_date = datetime . date ( 2005 , 1 , 1 )) >>> e . save ( force_insert = True ) リレーションシップを定義するモデルのキーワード引数を指定する必要はないことに注意してください。上の ... ... を生成するためには、作成するモデルのクラスにキーワード引数を渡してインスタンス化し、そのデータをデータベースに保存するために save() を呼び出します。 以下はモデルがDjangoアプリ blog 内の models.py ファイルに存在する場合の例です。 >>> from blog.models im ... port Blog >>> b = Blog ( name = "Beatles Blog" , tagline = "All the latest Beatles news." ) >>> b . save () この例では内部で INSERT SQL 文が処理されます。明示的に save() を呼ぶまで Django はデータベースを操作しません。 save() メ ... ソッドは値を返しません。 参考 save() はここには記述されていない多数の高度なオプションを持ちます。詳細については save() の項目を参照してください。 To create a ... ... ナルを使用する前に モデルメソッドをオーバーライド することを検討してください。 警告 これらのシグナルの多くは、 __init__() や save() のような、自分のコードでオーバーライドできる様々なモデルメソッドによって送信されます。 これらのメソッドをモデルでオーバー ... リを実行するべきではありません。なぜなら、クエリセットのイテレート中に返される各インスタンスに対して実行されるからです。 pre_save django.db.models.signals. pre_save これは、モデルの save() メソッドの始まりに送信されます。 このシグナルとともに送信される ... レコードをクエリしたり変更したりするべきではありません。 using 使用されているデータベースのエイリアス。 update_fields Model.save() に渡された更新するフィールドのセット、もしくは update_fields が save() に渡されなかった場合は None 。 post_save django.d ... ... る操作の例です。 Publication インスタンスをいくつか作ります: >>> p1 = Publication ( title = "The Python Journal" ) >>> p1 . save () >>> p2 = Publication ( title = "Science News" ) >>> p2 . save () >>> p3 = Publication ( title = "Science Weekly" ) >>> ... p3 . save () Article を一つ作ります: >>> a1 = Article ( headline = "Django lets you build web apps easily" ) 保存するまでは Publicat ... easily>" needs to have a value for field "id" before this many-to-many relationship can be used. 保存しましょう! >>> a1 . save () Article と Publication を関連付けます: >>> a1 . publications . add ( p1 ) Article をもう一つ作り、それが publications に ... ... 唯一必須のもの、それはモデルが定義するデータベースフィールドのリストです。フィールドはクラスの属性として定義します。 clean , save , delete のような モデル API と衝突するフィールド名は付けられないことに注意してください。 実装例: from django.db import mo ... harField ( max_length = 1 , choices = SHIRT_SIZES ) >>> p = Person ( name = "Fred Flintstone" , shirt_size = "L" ) >>> p . save () >>> p . shirt_size 'L' >>> p . get_shirt_size_display () 'Large' 列挙型クラスを用いて、 choices を簡潔に定義することもで ... th = 100 ) >>> fruit = Fruit . objects . create ( name = "Apple" , color = "Green" ) >>> fruit . name = "Pear" >>> fruit . save () >>> Fruit . objects . order_by ( "name" ) . values_list ( "name" , "color" ) <QuerySet [('Apple', 'Green'), ('Pear', 'G ... 2026年10月08日