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 , 7 years ago
Type: | Uncategorized → Cleanup/optimization |
---|
comment:2 by , 7 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
Duplicate of #6785.