... jects . order_by ( "name__length" ) # Boolean expression can be used directly in filters. >>> from django.db.models import Exists , OuterRef >>> Company . objects . filter ( ... Exists ( Employee . objects . filter ( company = OuterRef ( "pk" ), salary ... ます。 (スライスの代わりに get() を使うと、 Subquery 内でクエリセットが使われるまで OuterRef が解決できないため失敗します) Exists() サブクエリ class Exists ( queryset ) [ソース] Exists は SQL の EXISTS ステートメントを使う サブクエリ のサブクラスです。 ... とえば、各投稿に直近1日以内のコメントがあるかどうかのアノテーションを付けることができます: >>> from django.db.models import Exists , OuterRef >>> from datetime import timedelta >>> from django.utils import timezone >>> one_day_ago = timezone . now () - ... ... ith = "Paul" ), then = "name" ) >>> # Condition can be created using boolean expressions. >>> from django.db.models import Exists , OuterRef >>> non_unique_account_type = ( ... Client . objects . filter ( ... account_type = OuterRef ( "account_type" ), ... ... ) ... . exclude ( pk = OuterRef ( "pk" )) ... . values ( "pk" ) ... ) >>> When ( Exists ( non_unique_account_type ), then = Value ( "non unique" )) >>> # Condition can be created using lookup expressions. >>> f ... count_type" ), ... ) ... . exclude ( pk = OuterRef ( "pk" )) ... . values ( "pk" ) ... ) >>> Client . objects . filter ( ~ Exists ( non_unique_account_type )) SQL文では、次のように評価されます: SELECT ... FROM client c0 WHERE NOT EXISTS ( SELECT c1 . i ... ... g queryset bitwise combination when used with subqueries that began manifesting in Django 3.2, due to a separate fix using Exists to exclude() multi-valued relationships ( #32650 ). Fixed a bug in Django 3.2 where variable lookup errors were logged whe ... st would crash when deleting objects filtered against multi-valued relationships ( #32682 ). The admin changelist now uses Exists() instead of QuerySet.distinct() because calling delete() after distinct() is not allowed in Django 3.2 to address a data ... ... RLField form fields which use these validators all strip the input, so the possibility of newlines entering your data only exists if you are using these validators outside of the form fields. The undocumented, internally unused validate_integer() funct ... d crash during makemigrations if a migrations module either is missing __init__.py or is a file ( #24848 ). Fixed QuerySet.exists() returning incorrect results after annotation with Count() ( #24835 ). Corrected HStoreField.has_changed() ( #24844 ). Re ... ... ed a regression in Django 3.0 where QuerySet.exists() crashed if a queryset contained an aggregation over a Subquery() ( #31109 ). Fixed a regression in Django 3.0 that cause ... ... cts . all (): print ( e . headline ) メモ: 1つ以上の結果が存在するかどうかを判定したいだけなら、これは使わないでください。 exists() を使った方が効率的です。 非同期イテレーション. QuerySet は async for を使ってイテレートすることもできます: async for e i ... もしクエリの結果が少なくとも1つ存在するかどうかを確認したいだけであれば(そして実際のオブジェクトを必要としないのであれば)、 exists() を使うべきです。 QuerySet の Pickle 化 QuerySet を pickle 化するとき、pickle化の前にすべての結果がメモリにロードされるよ ... der_by ( "name" ) In addition, only LIMIT , OFFSET , COUNT(*) , ORDER BY , and specifying columns (i.e. slicing, count() , exists() , order_by() , and values() / values_list() ) are allowed on the resulting QuerySet . Further, databases place restricti ... ... ... t to True , did not truncate the overwritten file content ( #36191 ). Fixed a regression in Django 5.1 where the count and exists methods of ManyToManyField related managers would always return 0 and False when the intermediary model back references us ... ... a regression in Django 3.0 where QuerySet.values() and values_list() crashed if a queryset contained an aggregation and an Exists() annotation on Oracle ( #31584 ). Fixed a regression in Django 3.0 where all resolved Subquery() expressions were conside ... ... _queryset ( request ) if qs . filter ( birthday__gte = date ( 1980 , 1 , 1 ), birthday__lte = date ( 1989 , 12 , 31 ), ) . exists (): yield ( "80s" , _ ( "in the eighties" )) if qs . filter ( birthday__gte = date ( 1990 , 1 , 1 ), birthday__lte = date ... ( 1999 , 12 , 31 ), ) . exists (): yield ( "90s" , _ ( "in the nineties" )) フィールド名と明示的な FieldListFilter を使用する 最後に、フィールドに使用する ... 2026年10月08日