Slicing on queryset not working when using .last() after queryset

53 views
Skip to first unread message

SHIVAM JINDAL

unread,
Sep 24, 2018, 7:49:08 AM9/24/18
to django-d...@googlegroups.com
Hi,

I am making a queryset by slicing it to return only first 2 objects but When I am using .last() on this queryset then it is not applying slicing

Pharmaceutical.objects.order_by('id').values('id')
>>> [{'id': 2L}, {'id': 3L}, {'id': 4L}, {'id': 5L}]

>>> Pharmaceutical.objects.order_by('id')[:2].values('id')
[{'id': 2L}, {'id': 3L}]

>>> Pharmaceutical.objects.order_by('id')[:2].values('id').last()
{'id': 5L} # But this should be 3

--
Thanks,

Regards
Shivam Jindal

Vasili Korol

unread,
Sep 24, 2018, 8:12:02 AM9/24/18
to Django developers (Contributions to Django itself)
I can confirm this issue on Django 1.11.15 (Python 3.4.3, MySQL 5.5.61). Calling 'last()' on a slice produces wrong result:

>>> News.objects.all().order_by('id')[:5].values('id')
<QuerySet [{'id': 1}, {'id': 2}, {'id': 3}, {'id': 4}, {'id': 5}]>

>>>
News.objects.all().order_by('id')[:5][4].id
5

>>> #But calling 'last' gives us wrong id:
>>>
News.objects.all().order_by('id')[:5].last()
11


понедельник, 24 сентября 2018 г., 13:49:08 UTC+2 пользователь Shivam Jindal написал:

SHIVAM JINDAL

unread,
Sep 24, 2018, 8:23:06 AM9/24/18
to django-d...@googlegroups.com
I am facing this on Django 1.9, Python 2.7

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/d2a29c84-00f8-41e9-85fa-a8bf20985218%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Thanks,

Regards
Shivam jindal

Vasili Korol

unread,
Sep 24, 2018, 8:27:15 AM9/24/18
to Django developers (Contributions to Django itself)
Okay, this seems to produce TypeError in Django 2.0. See here: 
Reply all
Reply to author
Forward
0 new messages