File tree Expand file tree Collapse file tree 1 file changed +11
-4
lines changed Expand file tree Collapse file tree 1 file changed +11
-4
lines changed Original file line number Diff line number Diff line change @@ -335,11 +335,18 @@ return a list of every *second* object of the first 10::
335
335
Entry.objects.all()[:10:2]
336
336
337
337
To retrieve a *single* object rather than a list
338
- (e.g. ``SELECT foo FROM bar LIMIT 1``), slice the ``QuerySet`` to ``[:1]`` and
339
- call ``get()`` on that . For example, this returns the first ``Entry`` in the
340
- database, after ordering entries alphabetically by headline::
338
+ (e.g. ``SELECT foo FROM bar LIMIT 1``), using a simple index instead of a
339
+ slice . For example, this returns the first ``Entry`` in the database, after
340
+ ordering entries alphabetically by headline::
341
341
342
- Entry.objects.order_by('headline')[:1].get()
342
+ Entry.objects.order_by('headline')[0]
343
+
344
+ This is equivalent to::
345
+
346
+ Entry.objects.order_by('headline')[0:1].get()
347
+
348
+ Note that either of these two examples will raise ``DoesNotExist`` if no
349
+ objects match the given criteria.
343
350
344
351
QuerySet methods that return new QuerySets
345
352
------------------------------------------
You can’t perform that action at this time.
0 commit comments