@@ -29,7 +29,7 @@ CRUD operations. This guide shows how to use the following ``QuerySet``
29
29
methods:
30
30
31
31
- :ref:`create() <django-crud-insert>`
32
- - :ref:`filter() <django-crud-read>`
32
+ - :ref:`filter() and get() <django-crud-read>`
33
33
- :ref:`update() <django-crud-modify>`
34
34
- :ref:`delete() <django-crud-delete>`
35
35
@@ -127,11 +127,8 @@ that specifies which documents to retrieve, as an argument to the ``filter()`` m
127
127
Alternatively, you can call the ``get()`` method to retrieve a single document
128
128
that matches your query.
129
129
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
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
135
132
136
133
The following example calls the ``filter()`` method on your ``Movie`` objects
137
134
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:
158
155
<Movie: First Person Plural>, <Movie: Just, Melvin: Just Evil>, <Movie: Sound and Fury>,
159
156
<Movie: Peppermint Candy>]>
160
157
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()``.
161
186
162
187
.. _django-crud-modify:
163
188
0 commit comments