Opened 7 years ago

Closed 7 years 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

Description

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/query.py b/django/db/models/query.py
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -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 by Tim Graham, 7 years ago

Type: UncategorizedCleanup/optimization

Duplicate of #6785.

comment:2 by Дилян Палаузов, 7 years ago

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