Thursday, February 25, 2016

Re: LIMIT 1 on model.objects.get()

On Thu, Feb 25, 2016 at 07:10:31AM -0800, Matthew Crowson wrote:
> 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?

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20160225153101.GM880%40koniiiik.org.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment