MCPcopy
hub / github.com/django/django / get

Method get

django/db/models/query.py:659–692  ·  view source on GitHub ↗

Perform the query and return a single object matching the given keyword arguments.

(self, *args, **kwargs)

Source from the content-addressed store, hash-verified

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)

Callers 15

get_or_createMethod · 0.95
_restore_contextFunction · 0.45
sendMethod · 0.45
asendMethod · 0.45
send_robustMethod · 0.45
asend_robustMethod · 0.45
_live_receiversMethod · 0.45
_route_dbMethod · 0.45
_field_data_typeMethod · 0.45
django_test_skipsMethod · 0.45

Calls 6

_chainMethod · 0.95
filterMethod · 0.95
NotSupportedErrorClass · 0.90
can_filterMethod · 0.80
order_byMethod · 0.80
set_limitsMethod · 0.80

Tested by

no test coverage detected