QuerySets don't support indexing. I'm not entirely sure why, but I
think the reason is to encourage more efficient database usage, since
naive usage of indexing would lead to many repeated LIMIT 1 OFFSET N
type queries. Forcing people to slice first encourages them to think
about exactly how many results they need and evaluate them all at
once.

On Thu, May 16, 2013 at 11:06 AM, Lee Trout <leetr...@gmail.com> wrote:
> Let me clarify that I would expect both qs[:1][0] and qs[0] to raise
> IndexError if there were no results.
>
>
>
> On Thu, May 16, 2013 at 11:05 AM, Lee Trout <leetr...@gmail.com> wrote:
>>
>> That's what I thought- But why not just qs[0]?
>>
>> Doesn't qs[:1] and qs[0] both cause a LIMIT 1 on the query? It seems that
>> the [:1] is unnecessary. I would expect both to raise IndexError.
>>
>>
>>
>> On Wed, May 15, 2013 at 11:39 PM, Alex Ogier <alex.og...@gmail.com> wrote:
>>>
>>> Significantly better. The latter method loads every single model in the
>>> queryset into Python, potentially the whole database!
>>>
>>> On May 15, 2013 9:24 PM, "Lee Trout" <leetr...@gmail.com> wrote:
>>>>
>>>> Is qs[:1][0] better form than list(qs)[0]?
>>>>
>>>>
>>>> On Wed, May 15, 2013 at 7:48 AM, Selwin Ong <selwin....@gmail.com>
>>>> wrote:
>>>>>
>>>>> I've updated the first() and last() to not accept any arguments. Please
>>>>> review it and let me know if there's anything else I need to change.
>>>>> Hopefully this can get merged in during the sprints and make it into 1.6 
>>>>> :).
>>>>>
>>>>> The pull request is here: https://github.com/django/django/pull/1056
>>>>>
>>>>> Best,
>>>>> Selwin
>>>>>
>>>>> On Monday, May 13, 2013 8:12:35 PM UTC+7, Michal Petrucha wrote:
>>>>>>
>>>>>> > > I initially modeled "first()" and "last()"'s behaviors to mimic
>>>>>> > > "latest()", but in this new pull request, you can pass multiple
>>>>>> > > field names
>>>>>> > > into "first()" and "last()" so it behaves like "order_by()". It's
>>>>>> > > more
>>>>>> > > flexible and requires less typing, but I wonder if we should just
>>>>>> > > get rid
>>>>>> > > of the optional field arguments and rely on "order_by" for
>>>>>> > > ordering. "There
>>>>>> > > should be one-- and preferably only one --obvious way to do it".
>>>>>> >
>>>>>> > Considering "There should be one-- and preferably only one --obvious
>>>>>> > way to
>>>>>> > do it", I definitely prefer to rely on order_by to do the ordering,
>>>>>> > not on
>>>>>> > first.
>>>>>> >
>>>>>> > .order_by('name').first()
>>>>>> >
>>>>>> > is clear and readable in my opinion.
>>>>>>
>>>>>> My thoughts exactly, we already have one method that does ordering, I
>>>>>> don't think it is necessary to make these methods incorporate that
>>>>>> functionality. If we did, we might argue that other QuerySet
>>>>>> operations could be supported as well and that would just result in a
>>>>>> bloated API. Especially if there's no performance gain (the QuerySet
>>>>>> would be cloned anyway), and it only saves a few lines of code.
>>>>>>
>>>>>> Also, skimming through this thread, I think there was a consensus on
>>>>>> first() and last() not taking any ordering arguments, i.e. the first
>>>>>> proposed syntax:
>>>>>>
>>>>>>     .filter(last_name__startswith='b').order_by('last_name').first()
>>>>>>
>>>>>> Michal
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Django developers" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to django-developers+unsubscr...@googlegroups.com.
>>>>> To post to this group, send email to
>>>>> django-developers@googlegroups.com.
>>>>> Visit this group at
>>>>> http://groups.google.com/group/django-developers?hl=en.
>>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Django developers" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to django-developers+unsubscr...@googlegroups.com.
>>>> To post to this group, send email to django-developers@googlegroups.com.
>>>> Visit this group at
>>>> http://groups.google.com/group/django-developers?hl=en.
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>
>>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Django developers" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to django-developers+unsubscr...@googlegroups.com.
>>> To post to this group, send email to django-developers@googlegroups.com.
>>> Visit this group at
>>> http://groups.google.com/group/django-developers?hl=en.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>>
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To post to this group, send email to django-developers@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-developers?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to