... デルを追跡し、モデルを扱うための高レベルで汎用的なインターフェースを提供します。 概要 ¶ contenttypes アプリケーションの中心にあるのは、 django.contrib.contenttypes.models.ContentType にある ContentType モデルです。 Content ... Type のインスタンスは、プロジェクトにインストールされたモデルに関する情報を表し、格納します。新しい ContentType のインスタンスは、新しいモデルがインストールされるたびに自動的に作成されます。 ContentType のインスタンスは ContentType が ... 表すモデルクラスを返すメソッドと、それらのモデルからオブジェクトを問い合わせるメソッドを持っています。また、 ContentType を操作するためのメソッドや、特定のモデルの ContentType のインスタンスを取得するためのメソッドを追加する カスタムマネージャ ... ... デルを追跡し、モデルを扱うための高レベルで汎用的なインターフェースを提供します。 概要 ¶ contenttypes アプリケーションの中心にあるのは、 django.contrib.contenttypes.models.ContentType にある ContentType モデルです。 Content ... Type のインスタンスは、プロジェクトにインストールされたモデルに関する情報を表し、格納します。新しい ContentType のインスタンスは、新しいモデルがインストールされるたびに自動的に作成されます。 ContentType のインスタンスは ContentType が ... 表すモデルクラスを返すメソッドと、それらのモデルからオブジェクトを問い合わせるメソッドを持っています。また、 ContentType を操作するためのメソッドや、特定のモデルの ContentType のインスタンスを取得するためのメソッドを追加する カスタムマネージャ ... ... e alias to the InconsistentMigrationHistory message raised by makemigrations and migrate ( #27089 ). Fixed the creation of ContentType and Permission objects for models of applications without migrations when calling the migrate command with no migrations t ... ded the already applied migration state changes in the Apps instance provided to the pre_migrate signal receivers to allow ContentType renaming to be performed on model rename ( #27100 ). Reallowed subclassing UserCreationForm without USERNAME_FIELD in Meta ... ... e alias to the InconsistentMigrationHistory message raised by makemigrations and migrate ( #27089 ). Fixed the creation of ContentType and Permission objects for models of applications without migrations when calling the migrate command with no migrations t ... ded the already applied migration state changes in the Apps instance provided to the pre_migrate signal receivers to allow ContentType renaming to be performed on model rename ( #27100 ). Reallowed subclassing UserCreationForm without USERNAME_FIELD in Meta ... ... ように作成できます。 from myapp.models import BlogPost from django.contrib.auth.models import Permission from django.contrib.contenttypes.models import ContentType content_type = ContentType . objects . get_for_model ( BlogPost ) permission = Permission . obj ... できます。 プロキシモデルには独自のコンテンツタイプが必要です プロキシモデルのパーミッション を作成したい場合は、次のように ContentTypeManager.get_for_model() に for_concrete_model=False を渡して、適切な ContentType を取得してください。 content_type = Conten ... m再取得することです。実装例を以下に示します。 from django.contrib.auth.models import Permission , User from django.contrib.contenttypes.models import ContentType from django.shortcuts import get_object_or_404 from myapp.models import BlogPost def user_gains ... ... ds.mysql" , "USER" : "mysql_user" , "PASSWORD" : "bacon" , }, } 次に、ルーティングを処理する必要があります。まず、 auth と contenttypes アプリのクエリを auth_db に送信するルーターが必要です ( auth モデルは ContentType にリンクされているので、同じデータベース ... 格納されている必要があります): class AuthRouter : """ A router to control all database operations on models in the auth and contenttypes applications. """ route_app_labels = { "auth" , "contenttypes" } def db_for_read ( self , model , ** hints ): """ Attempt ... s to read auth and contenttypes models go to auth_db. """ if model . _meta . app_label in self . route_app_labels : return "auth_db" return None def db_f ... ... ように作成できます。 from myapp.models import BlogPost from django.contrib.auth.models import Permission from django.contrib.contenttypes.models import ContentType content_type = ContentType . objects . get_for_model ( BlogPost ) permission = Permission . obj ... できます。 プロキシモデルには独自のコンテンツタイプが必要です プロキシモデルのパーミッション を作成したい場合は、次のように ContentTypeManager.get_for_model() に for_concrete_model=False を渡して、適切な ContentType を取得してください。 content_type = Conten ... m再取得することです。実装例を以下に示します。 from django.contrib.auth.models import Permission , User from django.contrib.contenttypes.models import ContentType from django.shortcuts import get_object_or_404 from myapp.models import BlogPost def user_gains ... ... ds.mysql" , "USER" : "mysql_user" , "PASSWORD" : "bacon" , }, } 次に、ルーティングを処理する必要があります。まず、 auth と contenttypes アプリのクエリを auth_db に送信するルーターが必要です ( auth モデルは ContentType にリンクされているので、同じデータベース ... 格納されている必要があります): class AuthRouter : """ A router to control all database operations on models in the auth and contenttypes applications. """ route_app_labels = { "auth" , "contenttypes" } def db_for_read ( self , model , ** hints ): """ Attempt ... s to read auth and contenttypes models go to auth_db. """ if model . _meta . app_label in self . route_app_labels : return "auth_db" return None def db_f ... ... 他のリレーション先フィールドは、少し違う扱いになります: <object pk= "27" model= "auth.permission" > <!-- ... --> <field to= "contenttypes.contenttype" name= "content_type" rel= "ManyToOneRel" > 9 </field> <!-- ... --> </object> この例では、PK 27の auth.Permis ... sion オブジェクトが、PK 9の contenttypes.ContentType インスタンスに対する外部キーを持つように指定しています。 多対多のリレーションはそれらを結びつけるモデルに対し ... ライズするというものです。この戦略はほとんどのオブジェクトに対してうまく機能しますが、いくつかの状況で困難を引き起こします。 ContentType を参照する外部キーを持つオブジェクトのリストの場合を考えてみてください。もしcontent type を参照するオブジェクトをシリアライ ... ... 他のリレーション先フィールドは、少し違う扱いになります: <object pk= "27" model= "auth.permission" > <!-- ... --> <field to= "contenttypes.contenttype" name= "content_type" rel= "ManyToOneRel" > 9 </field> <!-- ... --> </object> この例では、PK 27の auth.Permis ... sion オブジェクトが、PK 9の contenttypes.ContentType インスタンスに対する外部キーを持つように指定しています。 多対多のリレーションはそれらを結びつけるモデルに対し ... ライズするというものです。この戦略はほとんどのオブジェクトに対してうまく機能しますが、いくつかの状況で困難を引き起こします。 ContentType を参照する外部キーを持つオブジェクトのリストの場合を考えてみてください。もしcontent type を参照するオブジェクトをシリアライ ... 2026年10月08日