Browse files

Added more examples to the get_object_or_404 documentation.

  • Loading branch information...
1 parent 170eedf commit ebfa508fa32b27de6f3e2ca04c81b46408b0506e @bmispelon bmispelon committed Nov 19, 2013
Showing with 30 additions and 3 deletions.
  1. +30 −3 docs/topics/http/shortcuts.txt
33 docs/topics/http/shortcuts.txt
@@ -261,9 +261,10 @@ Required arguments
- A :class:`~django.db.models.Model`, :class:`~django.db.models.Manager` or
- :class:`~django.db.models.query.QuerySet` instance from which to get the
- object.
+ A :class:`~django.db.models.Model` class,
+ a :class:`~django.db.models.Manager`,
+ or a :class:`~django.db.models.query.QuerySet` instance from which to get
+ the object.
Lookup parameters, which should be in the format accepted by ``get()`` and
@@ -290,6 +291,32 @@ This example is equivalent to::
except MyModel.DoesNotExist:
raise Http404
+The most common use case is to pass a :class:`~django.db.models.Model`, as
+shown above. However, you can also pass a
+:class:`~django.db.models.query.QuerySet` instance::
+ queryset = Book.objects.filter(title__startswith='M')
+ get_object_or_404(queryset, pk=1)
+The above example is a bit contrived since it's equivalent to doing::
+ get_object_or_404(Book, title__startswith='M', pk=1)
+but it can be useful if you are passed the ``queryset`` variable from somewhere
+Finally, you can also use a :class:`~django.db.models.Manager`. This is useful
+for example if you have a
+:ref:`custom manager<custom-managers>`::
+ get_object_or_404(Book.dahl_objects, title='Matilda')
+You can also use
+:class:`related managers<django.db.models.fields.related.RelatedManager>`::
+ author = Author.objects.get(name='Roald Dahl')
+ get_object_or_404(author.book_set, title='Matilda')
Note: As with ``get()``, a
:class:`~django.core.exceptions.MultipleObjectsReturned` exception
will be raised if more than one object is found.

0 comments on commit ebfa508

Please sign in to comment.