Perform the query and return a single object matching the given keyword arguments.
(self, *args, **kwargs)
| 657 | return await sync_to_async(self.count)() |
| 658 | |
| 659 | def get(self, *args, **kwargs): |
| 660 | """ |
| 661 | Perform the query and return a single object matching the given |
| 662 | keyword arguments. |
| 663 | """ |
| 664 | if self.query.combinator and (args or kwargs): |
| 665 | raise NotSupportedError( |
| 666 | "Calling QuerySet.get(...) with filters after %s() is not " |
| 667 | "supported." % self.query.combinator |
| 668 | ) |
| 669 | clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs) |
| 670 | if self.query.can_filter() and not self.query.distinct_fields: |
| 671 | clone = clone.order_by() |
| 672 | limit = None |
| 673 | if ( |
| 674 | not clone.query.select_for_update |
| 675 | or connections[clone.db].features.supports_select_for_update_with_limit |
| 676 | ): |
| 677 | limit = MAX_GET_RESULTS |
| 678 | clone.query.set_limits(high=limit) |
| 679 | num = len(clone) |
| 680 | if num == 1: |
| 681 | return clone._result_cache[0] |
| 682 | if not num: |
| 683 | raise self.model.DoesNotExist( |
| 684 | "%s matching query does not exist." % self.model._meta.object_name |
| 685 | ) |
| 686 | raise self.model.MultipleObjectsReturned( |
| 687 | "get() returned more than one %s -- it returned %s!" |
| 688 | % ( |
| 689 | self.model._meta.object_name, |
| 690 | num if not limit or num < limit else "more than %s" % (limit - 1), |
| 691 | ) |
| 692 | ) |
| 693 | |
| 694 | async def aget(self, *args, **kwargs): |
| 695 | return await sync_to_async(self.get)(*args, **kwargs) |
no test coverage detected