... et はイテラブルで、初めてイテレートした時にデータベースのクエリを実行します。たとえば、これはデータベースにある全エントリのheadline属性を出力するプログラムです: for e in Entry . objects . all (): print ( e . headline ) メモ: 1つ以上の結果が存在するかどうか ... も少なくとも1つ以上の結果が含まれれば True となり、そうでなければ False になります。例えば: if Entry . objects . filter ( headline = "Test" ): print ( "There is at least one Entry with the headline Test" ) 注意: もしクエリの結果が少なくとも1つ存在するか ... るSQLステートメントでは AND によって結合され、全体が NOT() によって囲まれます。 この例では pub_date が 2005-1-3より新しく、 headline が "Hello" であるようなエントリーを除外しています: Entry . objects . exclude ( pub_date__gt = datetime . date ( 2005 , 1 , ... ... ilField () def __str__ ( self ): return f " { self . first_name } { self . last_name } " class Article ( models . Model ): headline = models . CharField ( max_length = 100 ) pub_date = models . DateField () reporter = models . ForeignKey ( Reporter , on_ ... delete = models . CASCADE ) def __str__ ( self ): return self . headline class Meta : ordering = [ "headline" ] 以下は、Python API の機能を使って実行できる操作の例です。 複数の Reporter を作成する ... il = "paul@example.com" ) >>> r2 . save () Article を作成する: >>> from datetime import date >>> a = Article ( id = None , headline = "This is a test" , pub_date = date ( 2005 , 7 , 27 ), reporter = r ) >>> a . save () >>> a . reporter . id 1 >>> a . rep ... ... f ): return self . name class Entry ( models . Model ): blog = models . ForeignKey ( Blog , on_delete = models . CASCADE ) headline = models . CharField ( max_length = 255 ) body_text = models . TextField () pub_date = models . DateField () mod_date = mo ... = models . IntegerField ( default = 0 ) rating = models . IntegerField ( default = 5 ) def __str__ ( self ): return self . headline オブジェクトを作成する Djangoでは、データベースのテーブルデータをPythonオブジェクトで表現するために、直感的な仕組みを採用し ... 。そのため、複数のフィルタを連結することが可能です。たとえば、次のように書くことができます。 >>> Entry . objects . filter ( headline__startswith = "What" ) . exclude ( ... pub_date__gte = datetime . date . today () ... ) . filter ( pub_date__gte = datetim ... ... gth = 30 ) class Meta : ordering = [ "title" ] def __str__ ( self ): return self . title class Article ( models . Model ): headline = models . CharField ( max_length = 100 ) publications = models . ManyToManyField ( Publication ) class Meta : ordering = ... [ "headline" ] def __str__ ( self ): return self . headline 以下は、Python API の機能を使って実行できる操作の例です。 Publication イン ... p2 . save () >>> p3 = Publication ( title = "Science Weekly" ) >>> p3 . save () Article を一つ作ります: >>> a1 = Article ( headline = "Django lets you build web apps easily" ) 保存するまでは Publication と関連付けることはできません: >>> a1 . publications ... ... モデル を使うと: >>> entry = Entry . objects . get ( id = 10 ) 上記は以下よりも高速です: >>> entry = Entry . objects . get ( headline = "News Item Title" ) これは、 id がデータベースによってインデックス化されていて、ユニークだと保証されているからです。 以下 ... のようにすると非常に遅くなる恐れがあります: >>> entry = Entry . objects . get ( headline__startswith = "News" ) まず第一に、 headline はインデックス化されておらず、データベースのデータ取り出しを遅くします。 Seco ... , use the bulk_create() method to reduce the number of SQL queries. For example: Entry . objects . bulk_create ( [ Entry ( headline = "This is a test" ), Entry ( headline = "This is only a test" ), ] ) ...これは下記よりも適切です: Entry . objects . creat ... ... ate the form class. >>> class ArticleForm ( ModelForm ): ... class Meta : ... model = Article ... fields = [ "pub_date" , "headline" , "content" , "reporter" ] ... # Creating a form to add an article. >>> form = ArticleForm () # Creating a form to change ... rm from myapp.models import Article class ArticleForm ( ModelForm ): class Meta : model = Article fields = [ "pub_date" , "headline" , "content" , "reporter" , "slug" ] field_classes = { "slug" : MySlugFormField , } もしくは: from django.forms import Mod ... db_field . formfield ( ** kwargs ) class ArticleForm ( ModelForm ): class Meta : model = Article fields = [ "pub_date" , "headline" , "content" , "reporter" , "slug" ] formfield_callback = formfield_for_dbfield 最後に、フィールドを完全に -- タイプ、バリ ... ... th = 70 ) def __str__ ( self ): return self . full_name class Article ( models . Model ): pub_date = models . DateField () headline = models . CharField ( max_length = 200 ) content = models . TextField () reporter = models . ForeignKey ( Reporter , on_d ... elete = models . CASCADE ) def __str__ ( self ): return self . headline モデルのインストール 次に、データベーステーブルを自動で作成する Django のコマンドラインユーティリティを実行します。 / ... query does not exist. # Create an article. >>> from datetime import date >>> a = Article ( ... pub_date = date . today (), headline = "Django is cool" , content = "Yeah." , reporter = r ... ) >>> a . save () # Now the article is in the database. >>> Arti ... ... されます。カラムに対して降順のインデックスを定義するには、フィールド名の前にハイフンを追加します。 たとえば、 Index(fields=['headline', '-pub_date']) は (headline, pub_date DESC) を持つ SQL を作成します。 name Index. name インデックスの名前。 name が与えら ... するクエリに対して、インデックスのみのスキャンが利用できるようになります。 例: Index ( name = "covering_index" , fields = [ "headline" ], include = [ "pub_date" ]) この例は、データをインデックスからのみ取得しながら、 headline のフィルタリングと、 pub_date ... ... 用例: class Publication ( models . Model ): title = models . CharField ( max_length = 30 ) class Article ( models . Model ): headline = models . CharField ( max_length = 100 ) publications = models . ManyToManyField ( Publication ) >>> article = Article . ... objects . create ( headline = "NASA uses Python" ) >>> article . publications . create ( title = "The Python Journal" ) <Publication: Publication obje ... ingAgg ( ... "publications__title" , ... delimiter = ", " , ... order_by = "publications__title" , ... ) ... ) . values ( "headline" , "publication_names" ) <QuerySet [{ 'headline': 'NASA uses Python', 'publication_names': 'Science News, The Python Journ ... ... lue ( 4 )), ... ) ... ) PostgreSQLのドキュメントに、 different rank normalization options についての詳細があります。 SearchHeadline class SearchHeadline ( expression , query , config = None , start_sel = None , stop_sel = None , max_words = None , min_wo ... highlighting search results に関する詳細が記載されています。 使用例: >>> from django.contrib.postgres.search import SearchHeadline , SearchQuery >>> query = SearchQuery ( "red tomato" ) >>> entry = Entry . objects . annotate ( ... headline = SearchHeadl ... "body_text" , ... query , ... start_sel = "<span>" , ... stop_sel = "</span>" , ... ), ... ) . get () >>> print ( entry . headline ) Sandwich with <span>tomato</span> and <span>red</span> cheese. config パラメータの説明については、 検索設定を変更する を ... 2026年10月08日