Opened 6 months ago

Closed 6 months ago

#28681 closed Cleanup/optimization (duplicate)

QuerySet.get() implies LIMIT 2

Reported by: Дилян Палаузов Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


If the database finds at least two results upon QuerySet.get() it can stop working, and Django will raise anyway MultipleObjectsReturned .

diff --git a/django/db/models/ b/django/db/models/
--- a/django/db/models/
+++ b/django/db/models/
@@ -370,9 +370,10 @@ class QuerySet(object):
         clone = self.filter(*args, **kwargs)
         if self.query.can_filter() and not self.query.distinct_fields:
             clone = clone.order_by()
+        clone = clone[:2]
         num = len(clone)
         if num == 1:
-            return clone._result_cache[0]
+            return clone[0]
         if not num:
             raise self.model.DoesNotExist(
                 "%s matching query does not exist." %

Change History (2)

comment:1 Changed 6 months ago by Tim Graham

Type: UncategorizedCleanup/optimization

Duplicate of #6785.

comment:2 Changed 6 months ago by Дилян Палаузов

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top