LIMIT 1 on model.objects.get()

20 views
Skip to first unread message

Matthew Crowson

unread,
Feb 25, 2016, 10:17:10 AM2/25/16
to Django users
When Django creates the SQL to get an object from model.objects.get() it
doesn't look like SQL is adding a "LIMIT 1" to the end. As the get method
is always intended to return a single item, would adding that to Django's
SQL speed things up?

Karen Tracey

unread,
Feb 25, 2016, 10:20:37 AM2/25/16
to django...@googlegroups.com
This would hide errors in cases where there are more than one. Currently you will get an exception raised if you try a get() and more than a single result is returned, if a LIMIT 1 were added to the sql then this error situation would be hidden from developers.







 

Michal Petrucha

unread,
Feb 25, 2016, 10:31:57 AM2/25/16
to django...@googlegroups.com
In addition to what Karen said, you might want to consider using
qs.first() instead, which does what you describe, AFAIK – assuming, of
course, that you don't need the extra check for multiple returned
objects that you get with qs.get():
https://docs.djangoproject.com/en/1.9/ref/models/querysets/#first

Cheers,

Michal
signature.asc
Reply all
Reply to author
Forward
0 new messages