Skip to main content

get_object_or_404

Use get() to return an object, or raise an Http404 exception if the object does not exist. klass may be a Model, Manager, or QuerySet object. All other passed arguments and keyword arguments are used in the get() query. Like with QuerySet.get(), MultipleObjectsReturned is raised if more than one object is found.

def get_object_or_404(
klass: Model, Manager, or QuerySet,
*args: Any,
**kwargs: Any
) - > Model instance

Use get() to return an object, or raise an Http404 exception if the object does not exist.

Parameters

NameTypeDescription
klassModel, Manager, or QuerySetThe model class, manager, or queryset from which to retrieve the object.
*argsAnyPositional arguments passed directly to the get() method of the queryset.
**kwargsAnyLookup parameters used to filter the queryset, passed directly to the get() method.

Returns

TypeDescription
Model instanceThe single model instance retrieved from the database matching the provided query criteria.