... コアを使用してください。これは、好きなだけ深くできます。制限はありません。たとえば: >>> r . article_set . filter ( headline__startswith = "This" ) <QuerySet [<Article: This is a test>]> # Find all Articles for any Reporter whose first name is "John". >>> Art ... objects . filter ( article = a ) <QuerySet [<Reporter: John Smith>]> >>> Reporter . objects . filter ( article__headline__startswith = "This" ) <QuerySet [<Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>]> >>> Reporter . objects . fi ... lter ( article__headline__startswith = "This" ) . distinct () <QuerySet [<Reporter: John Smith>]> 逆方向のカウントは distinct() と連動して動作します: >>> Repor ... ... jango lets you build web apps easily>, <Article: NASA uses Python>]> >>> Article . objects . filter ( publications__title__startswith = "Science" ) <QuerySet [<Article: NASA uses Python>, <Article: NASA uses Python>]> >>> Article . objects . filter ( publi ... cations__title__startswith = "Science" ) . distinct () <QuerySet [<Article: NASA uses Python>]> count() 関数は distinct() も尊重します: >>> Article . ... objects . filter ( publications__title__startswith = "Science" ) . count () 2 >>> Article . objects . filter ( publications__title__startswith = "Science" ) . distinct () . ... ... 、複数のフィルタを連結することが可能です。たとえば、次のように書くことができます。 >>> Entry . objects . filter ( headline__startswith = "What" ) . exclude ( ... pub_date__gte = datetime . date . today () ... ) . filter ( pub_date__gte = datetime . date ( 2 ... に独立した QuerySet が作られるため、保存したり何度も再利用したりできます。 例: >>> q1 = Entry . objects . filter ( headline__startswith = "What" ) >>> q2 = q1 . exclude ( pub_date__gte = datetime . date . today ()) >>> q3 = q1 . filter ( pub_date__gte = date ... 価される までは、Django は実際にはクエリを実行しません。次の例を見てください。 >>> q = Entry . objects . filter ( headline__startswith = "What" ) >>> q = q . filter ( pub_date__lte = datetime . date . today ()) >>> q = q . exclude ( body_text__icontains = " ... ... . objects . get ( place__pk = 1 ) <Restaurant: Demon Dogs the restaurant> >>> Restaurant . objects . filter ( place__name__startswith = "Demon" ) <QuerySet [<Restaurant: Demon Dogs the restaurant>]> >>> Restaurant . objects . exclude ( place__address__cont ... lace . objects . get ( restaurant = r ) <Place: Demon Dogs the place> >>> Place . objects . get ( restaurant__place__name__startswith = "Demon" ) <Place: Demon Dogs the place> 場所を削除した場合、そのレストランは削除されます ( OneToOneField が on_delete に ... QuerySet [<Waiter: Joe the waiter at Demon Dogs the restaurant>]> >>> Waiter . objects . filter ( restaurant__place__name__startswith = "Demon" ) <QuerySet [<Waiter: Joe the waiter at Demon Dogs the restaurant>]> ... ... jango.contrib.auth import get_user_model User = get_user_model () # Unsalted MD5/SHA1: User . objects . filter ( password__startswith = "md5$$" ) User . objects . filter ( password__startswith = "sha1$$" ) # Salted MD5/SHA1: User . objects . filter ( passw ... ord__startswith = "md5$" ) . exclude ( password__startswith = "md5$$" ) User . objects . filter ( password__startswith = "sha1$" ) . exclu ... de ( password__startswith = "sha1$$" ) # Crypt hasher: User . objects . filter ( password__startswith = "crypt$$" ) from django.db.models import Cha ... ... , 1 , 1 ), ... then = "account_type" , ... ) >>> # Complex conditions can be created using Q objects >>> When ( Q ( name__startswith = "John" ) | Q ( name__startswith = "Paul" ), then = "name" ) >>> # Condition can be created using boolean expressions. >> ... ... StatReloader ( #30323 ). Fixed a migration crash on Oracle and PostgreSQL when adding a check constraint with a contains , startswith , or endswith lookup (or their case-insensitive variant) ( #30408 ). Fixed a migration crash on Oracle and SQLite when add ... ... ように Model を渡すものです。しかし、 QuerySet インスタンスを渡すこともできます: queryset = Book . objects . filter ( title__startswith = "M" ) get_object_or_404 ( queryset , pk = 1 ) 上の例は下記と等価なので、少し不自然ではあります: get_object_or_404 ( Book ... , title__startswith = "M" , pk = 1 ) しかし、他の場所から QuerySet 変数を渡された場合には便利です。 最後に、 Manager を使うこともできます。これ ... ... があります。 注釈 キーのトランスフォームは、 contains 、 icontains 、 endswith 、 iendswith 、 iexact 、 regex 、 iregex 、 startswith 、および istartswith ルックアップとも連結できます。 警告 どのような文字列でも hstore 値のキーになり得るので、以下に列挙され ... Event: Soft play>, <Event: Pub trip>]> 境界を使ったクエリ 範囲フィールドはいくつかの追加ルックアップをサポートしています。 startswith 返されるオブジェクトは与えられた下界を持つ。ベースフィールドの有効なルックアップに連結できます。 >>> Event . objects . fil ... ter ( ages__startswith = 21 ) <QuerySet [<Event: Pub trip>]> endswith 返されるオブジェクトは与えられた上限を持つ。ベースフィールドの有効なルックア ... ... abase lookup API. >>> Reporter . objects . get ( id = 1 ) <Reporter: John Smith> >>> Reporter . objects . get ( full_name__startswith = "John" ) <Reporter: John Smith> >>> Reporter . objects . get ( full_name__contains = "mith" ) <Reporter: John Smith> >>> ... ar as you need, performing efficient # JOINs for you behind the scenes. # This finds all articles by a reporter whose name starts with "John". >>> Article . objects . filter ( reporter__full_name__startswith = "John" ) <QuerySet [<Article: Django is cool>] ... 2026年10月08日