On Mon, Jul 2, 2012 at 9:27 PM, Maxime Haineault <[email protected]>wrote:

> One of the common pitfall I come across way to often with novices is
> something like this:
>
>     def getFirstUser():
>         return User.objects.all()[0]
>
> It looks innocuous and often wont raise any exceptions in dev because you
> develop and test with data,
> so they slip easily in production.
>
>     def getFirstUser():
>         return User.objects.all().first()
>
> If the QuerySet is empty it should just return None instead of raising an
> exception.
>
> Other methods like .last() could probably also be useful, but getting the
> first element of a QuerySet
> is the pitfall I see the most often.
>
> Surprise Poney Quizz: which is the fastest way to get the first object of
> a QuerySet ?
>
> A) Using count:
>
>     qs = User.objects.all()
>     if qs.count() > 0:
>         return qs[0]
>     else:
>         return None
>
> B) Convert to list:
>
>     r = list(qs[:1])
>     if r:
>       return r[0]
>     return None
>
>
> C) Using len:
>
>     qs = User.objects.all()
>     if len(qs) > 0:
>         return qs[0]
>     else:
>         return None
>
> D) Try/except:
>
>     qs = User.objects.all()
>     try:
>         return qs[0]
>     except IndexError:
>         return None
>
> Those examples have been taken from this post on StackOverflow (which also 
> contains the answer):
>
> http://stackoverflow.com/questions/5123839/fastest-way-to-get-the-first-object-from-a-queryset-in-django
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django developers" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-developers/-/JleI1VvzPXIJ.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/django-developers?hl=en.
>

Actually, none of those is necessarily the correct answer.  The correct
answer is going to depend on your expected result (if no results are
significantly more common than results you'll probably want to use exists()
first), as well as your database and the nature of the query (some
databases with good optimizer recognize that that a query for equality on a
unique column and optimize appropriately, thus on such a database a LIMIT
is just extra bytes on the wire and for django to construct).

I'm -1 on a first() method, it strikes me as API bloat where there are 101
questions about what the most obvious API decision is, for example were we
to have such a method I'd assume it'd raise Model.DoesNotExist in the
absense of data, not return None.

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." -- Evelyn Beatrice Hall (summarizing Voltaire)
"The people's good is the highest law." -- Cicero

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to