... types アプリケーションの中心にあるのは、 django.contrib.contenttypes.models.ContentType にある ContentType モデルです。 ContentType のインスタンスは、プロジェクトにインストールされたモデルに ... 関する情報を表し、格納します。新しい 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 ... ... ように作成できます。 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 を参照するオブジェクトをシリアライ ... ... たいでしょう。最良の方法は、カスタムエクスポートビューにリダイレクトする小さなアクションを書くことです: from django.contrib.contenttypes.models import ContentType from django.http import HttpResponseRedirect def export_selected_objects ( modeladmin , request ... , queryset ): selected = queryset . values_list ( "pk" , flat = True ) ct = ContentType . objects . get_for_model ( queryset . model ) return HttpResponseRedirect ( "/export/?ct= %s &ids= %s " % ( ct . pk , "," ... はとても短いです。複雑なロジックはすべてエクスポートビューにあります。これは任意のタイプのオブジェクトを扱う必要があるため、 ContentType に関する処理が必要になります。 このビューの作成は、読者にとっての演習課題として残されています。 アクションをサイト全体で利 ... ... ーライドしたときに get_FOO_display() が正しく動かなくなる Django 2.2.7 のリグレッションを修正しました ( #31124 )。 カスタム ContentType 外部キーを持つ GenericForeignKey に対する``QuerySet.prefetch_related()`` がクラッシュする Django 3.0 のリグレッションを修正 ... ... プション inspectdb コマンドラインオプション loaddata コマンドラインオプション migrate コマンドラインオプション remove_stale_contenttypes コマンドラインオプション showmigrations コマンドラインオプション sqlflush コマンドラインオプション sqlmigrate コマンドライ ... oaddata コマンドラインオプション --include-partitions inspectdb コマンドラインオプション --include-stale-apps remove_stale_contenttypes コマンドラインオプション --include-views inspectdb コマンドラインオプション --indent dumpdata コマンドラインオプション -- ... ase のメソッド) app_directories.Loader (django.template.loaders のクラス) app_index_template (AdminSite の属性) app_label (ContentType の属性) (Options の属性) app_name (ResolverMatch の属性) app_names (ResolverMatch の属性) AppCommand (django.core.manageme ... ... go.contrib.admin.models import LogEntry from django.contrib.auth.models import Group, Permission, User from django.contrib.contenttypes.models import ContentType from django.contrib.sessions.models import Session from django.urls import resolve, reverse fro ... ... hich collide with Django's syntax, you can now use the verbatim block tag to avoid parsing the tag's content. Retrieval of ContentType instances associated with proxy models The methods ContentTypeManager.get_for_model() and ContentTypeManager.get_for_model ... ively for_concrete_model and for_concrete_models . By passing False using this argument it is now possible to retrieve the ContentType associated with proxy models. New view variable in class-based views context In all generic class-based views (or any clas ... attribute on the form. Behavior of syncdb with multiple databases syncdb now queries the database routers to determine if content types (when contenttypes is enabled) and permissions (when auth is enabled) should be created in the target database. Previous ... 2026年10月08日