... 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 = [ "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 . モデルの読み込みをカスタマイ ... ... 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" ... ... ッド ¶ オブジェクト(以下の 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 ... ... 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 はセーブポ ... ... する ¶ 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 はセーブポ ... 2026年10月08日