MCPcopy
hub / github.com/django/django / contains

Method contains

django/db/models/query.py:1441–1460  ·  view source on GitHub ↗

Return True if the QuerySet contains the provided obj, False otherwise.

(self, obj)

Source from the content-addressed store, hash-verified

1439 return await sync_to_async(self.exists)()
1440
1441 def contains(self, obj):
1442 """
1443 Return True if the QuerySet contains the provided obj,
1444 False otherwise.
1445 """
1446 self._not_support_combined_queries("contains")
1447 if self._fields is not None:
1448 raise TypeError(
1449 "Cannot call QuerySet.contains() after .values() or .values_list()."
1450 )
1451 try:
1452 if obj._meta.concrete_model != self.model._meta.concrete_model:
1453 return False
1454 except AttributeError:
1455 raise TypeError("'obj' must be a model instance.")
1456 if not obj._is_pk_set():
1457 raise ValueError("QuerySet.contains() cannot be used on unsaved objects.")
1458 if self._result_cache is not None:
1459 return obj in self._result_cache
1460 return self.filter(pk=obj.pk).exists()
1461
1462 async def acontains(self, obj):
1463 return await sync_to_async(self.contains)(obj=obj)

Callers 12

test_unsaved_objMethod · 0.45
test_obj_typeMethod · 0.45
test_valuesMethod · 0.45
test_basicMethod · 0.45
test_proxy_modelMethod · 0.45
test_wrong_modelMethod · 0.45
test_preparedMethod · 0.45
test_containsMethod · 0.45
test_equalsMethod · 0.45

Calls 4

filterMethod · 0.95
_is_pk_setMethod · 0.80
existsMethod · 0.45

Tested by 12

test_unsaved_objMethod · 0.36
test_obj_typeMethod · 0.36
test_valuesMethod · 0.36
test_basicMethod · 0.36
test_proxy_modelMethod · 0.36
test_wrong_modelMethod · 0.36
test_preparedMethod · 0.36
test_containsMethod · 0.36
test_equalsMethod · 0.36