[Django] #30151: last() and [] operator give different results

9 views
Skip to first unread message

Django

unread,
Feb 1, 2019, 6:52:52 AM2/1/19
to django-...@googlegroups.com
#30151: last() and [] operator give different results
-----------------------------------------+------------------------
Reporter: NMO13 | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
I have a model RSSFeed. To get the last element in my DB, I do:


{{{
RSSFeed.objects.last()
# Output: <RSSFeed: www.sooperarticles.com>
}}}


I slice it to get the first 10 element in the query


{{{
first_ten_feeds = RSSFeed.objects.all()[:10]
}}}


Using first and the bracket operator is consistent:

first_ten_feeds.first()
# Output: <RSSFeed: pressetext News>
first_ten_feeds[0]
# Output: <RSSFeed: pressetext News>

But using last and the bracket operator is not consistent:


{{{
first_ten_feeds[9]
# Output: <RSSFeed: FinanzNachrichten.de: Nachrichten zu IT-
Dienstleistungen>

first_ten_feeds.last()
# Output: <RSSFeed: www.sooperarticles.com>
}}}


Why? I expect to get the same result for last() and [] above.

RSSFeed.objects.last() and first_ten_feeds.last()

seem to give the same result but that does not make sense to me.

I already openend a SO question with the same content:
https://stackoverflow.com/questions/54477472/last-and-operator-give-
different-results
There I was suggested to open a bug.

--
Ticket URL: <https://code.djangoproject.com/ticket/30151>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 1, 2019, 6:53:32 AM2/1/19
to django-...@googlegroups.com
#30151: last() and [] operator give different results
-------------------------------+--------------------------------------

Reporter: NMO13 | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by NMO13:

Old description:

> I have a model RSSFeed. To get the last element in my DB, I do:
>

> {{{
> RSSFeed.objects.last()
> # Output: <RSSFeed: www.sooperarticles.com>
> }}}
>

> I slice it to get the first 10 element in the query
>

> {{{
> first_ten_feeds = RSSFeed.objects.all()[:10]
> }}}
>

> Using first and the bracket operator is consistent:
>
> first_ten_feeds.first()
> # Output: <RSSFeed: pressetext News>
> first_ten_feeds[0]
> # Output: <RSSFeed: pressetext News>
>
> But using last and the bracket operator is not consistent:
>

> {{{
> first_ten_feeds[9]
> # Output: <RSSFeed: FinanzNachrichten.de: Nachrichten zu IT-
> Dienstleistungen>
>
> first_ten_feeds.last()
> # Output: <RSSFeed: www.sooperarticles.com>
> }}}
>

> Why? I expect to get the same result for last() and [] above.
>

>
> RSSFeed.objects.last() and first_ten_feeds.last()
>
> seem to give the same result but that does not make sense to me.
>
> I already openend a SO question with the same content:
> https://stackoverflow.com/questions/54477472/last-and-operator-give-
> different-results
> There I was suggested to open a bug.

New description:

I have a model RSSFeed. To get the last element in my DB, I do:


{{{
RSSFeed.objects.last()
# Output: <RSSFeed: www.sooperarticles.com>
}}}


I slice it to get the first 10 element in the query


{{{
first_ten_feeds = RSSFeed.objects.all()[:10]
}}}


Using first and the bracket operator is consistent:

first_ten_feeds.first()
# Output: <RSSFeed: pressetext News>
first_ten_feeds[0]
# Output: <RSSFeed: pressetext News>

But using last and the bracket operator is not consistent:


{{{
first_ten_feeds[9]
# Output: <RSSFeed: FinanzNachrichten.de: Nachrichten zu IT-
Dienstleistungen>

first_ten_feeds.last()
# Output: <RSSFeed: www.sooperarticles.com>
}}}


Why? I expect to get the same result for last() and [] above.

RSSFeed.objects.last() and first_ten_feeds.last() seem to give the same
result but that does not make sense to me.

I already openend a SO question with the same content:
https://stackoverflow.com/questions/54477472/last-and-operator-give-
different-results
There I was suggested to open a bug.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30151#comment:1>

Django

unread,
Feb 1, 2019, 6:54:47 AM2/1/19
to django-...@googlegroups.com
#30151: last() and [] operator give different results
-------------------------------+--------------------------------------

Reporter: NMO13 | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by NMO13:

Old description:

> I have a model RSSFeed. To get the last element in my DB, I do:
>

> {{{
> RSSFeed.objects.last()
> # Output: <RSSFeed: www.sooperarticles.com>
> }}}
>

> I slice it to get the first 10 element in the query
>

> {{{
> first_ten_feeds = RSSFeed.objects.all()[:10]
> }}}
>

> Using first and the bracket operator is consistent:
>
> first_ten_feeds.first()
> # Output: <RSSFeed: pressetext News>
> first_ten_feeds[0]
> # Output: <RSSFeed: pressetext News>
>
> But using last and the bracket operator is not consistent:
>

> {{{
> first_ten_feeds[9]
> # Output: <RSSFeed: FinanzNachrichten.de: Nachrichten zu IT-
> Dienstleistungen>
>
> first_ten_feeds.last()
> # Output: <RSSFeed: www.sooperarticles.com>
> }}}
>

> Why? I expect to get the same result for last() and [] above.
>

>
> RSSFeed.objects.last() and first_ten_feeds.last() seem to give the same
> result but that does not make sense to me.
>
> I already openend a SO question with the same content:
> https://stackoverflow.com/questions/54477472/last-and-operator-give-
> different-results
> There I was suggested to open a bug.

New description:

I have a model RSSFeed. To get the last element in my DB, I do:


{{{
RSSFeed.objects.last()
# Output: <RSSFeed: www.sooperarticles.com>
}}}


I slice it to get the first 10 element in the query


{{{
first_ten_feeds = RSSFeed.objects.all()[:10]
}}}


Using first and the bracket operator is consistent:


{{{
first_ten_feeds.first()
# Output: <RSSFeed: pressetext News>
first_ten_feeds[0]
# Output: <RSSFeed: pressetext News>
}}}


But using last and the bracket operator is not consistent:


{{{
first_ten_feeds[9]
# Output: <RSSFeed: FinanzNachrichten.de: Nachrichten zu IT-
Dienstleistungen>

first_ten_feeds.last()
# Output: <RSSFeed: www.sooperarticles.com>
}}}


Why? I expect to get the same result for last() and [] above.

RSSFeed.objects.last() and first_ten_feeds.last() seem to give the same
result but that does not make sense to me.

I already openend a SO question with the same content:
https://stackoverflow.com/questions/54477472/last-and-operator-give-
different-results
There I was suggested to open a bug.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30151#comment:2>

Django

unread,
Feb 1, 2019, 8:06:13 AM2/1/19
to django-...@googlegroups.com
#30151: QuerySet.last() and [] operator give different results
-------------------------------------+-------------------------------------
Reporter: NMO13 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => Bug
* resolution: => invalid


Comment:

As far as I can tell from the code you gave, the slice is operating on an
unordered queryset. There's no guarantee as to what ordering the database
will return. `last()` gives ordering by primary key if the queryset isn't
ordered.

--
Ticket URL: <https://code.djangoproject.com/ticket/30151#comment:3>

Reply all
Reply to author
Forward
0 new messages