Return True if the QuerySet contains the provided obj, False otherwise.
(self, obj)
| 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) |