Skip to content

Commit 84bc486

Browse files
committed
add get() example
1 parent 6123720 commit 84bc486

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

source/interact-data/crud.txt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ CRUD operations. This guide shows how to use the following ``QuerySet``
2929
methods:
3030

3131
- :ref:`create() <django-crud-insert>`
32-
- :ref:`filter() <django-crud-read>`
32+
- :ref:`filter() and get() <django-crud-read>`
3333
- :ref:`update() <django-crud-modify>`
3434
- :ref:`delete() <django-crud-delete>`
3535

@@ -127,11 +127,8 @@ that specifies which documents to retrieve, as an argument to the ``filter()`` m
127127
Alternatively, you can call the ``get()`` method to retrieve a single document
128128
that matches your query.
129129

130-
.. TODO: (add to the previous paragraph) To view an example that calls the
131-
``get()`` method, see the Specify a Query guide.
132-
133-
Example
134-
~~~~~~~
130+
Return Multiple Documents Example
131+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
135132

136133
The following example calls the ``filter()`` method on your ``Movie`` objects
137134
to retrieve documents from the ``sample_mflix.movies`` collection. The query
@@ -158,6 +155,34 @@ returns ``Movie`` objects that represent movies released on January 1, 2000:
158155
<Movie: First Person Plural>, <Movie: Just, Melvin: Just Evil>, <Movie: Sound and Fury>,
159156
<Movie: Peppermint Candy>]>
160157

158+
Return One Document Example
159+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160+
161+
To retrieve only one document that matches your query criteria, call the
162+
``get()`` method and pass a query filter as an argument. The following example
163+
retrieves a document in which the ``title`` value is ``"Boyhood"``:
164+
165+
.. io-code-block::
166+
:copyable: true
167+
168+
.. input::
169+
:language: python
170+
171+
from sample_mflix.models import Movie
172+
173+
Movie.objects.get(title="Boyhood")
174+
175+
.. output::
176+
:language: none
177+
:visible: false
178+
179+
<Movie: Boyhood>
180+
181+
.. note::
182+
183+
If your query matches no documents or multiple documents, the ``get()``
184+
method generates an error. To retrieve one document from a query
185+
that might match multiple, chain the ``first()`` method to ``filter()``.
161186

162187
.. _django-crud-modify:
163188

0 commit comments

Comments
 (0)