Failed to understand syntax

38 views
Skip to first unread message

Mayur Bagul

unread,
Apr 12, 2019, 9:25:15 AM4/12/19
to Django users
Hello coders,

I'm here to understand line of code which im unable to understand how it work.
i'm looking forward to get help from you.

code is given below :


from django.shortcuts import render

from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)




- in the code above i'm looking forward to know about bold and highlighted part how it work ?

Test Bot

unread,
Apr 12, 2019, 10:01:49 AM4/12/19
to django...@googlegroups.com
The part you want to understand is same as ORDER BY clause in SQL. The slicing will provide only the top 5 results. By default the order_by sorts in ascending order only. But providing a "-" would make it to sort in descending order.

--
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...@googlegroups.com.
To post to this group, send email to django...@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/bbc7142c-bf46-4fb4-8e25-bbe7a2594c9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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...@googlegroups.com.
To post to this group, send email to django...@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/bbc7142c-bf46-4fb4-8e25-bbe7a2594c9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mayur Bagul

unread,
Apr 13, 2019, 3:38:26 AM4/13/19
to Django users
Hello Judge95,

thanks for useful information but i want to know how [:5] is used without . operator and how it is work.

thanking you.


On Friday, April 12, 2019 at 7:31:49 PM UTC+5:30, OnlineJudge95 wrote:
The part you want to understand is same as ORDER BY clause in SQL. The slicing will provide only the top 5 results. By default the order_by sorts in ascending order only. But providing a "-" would make it to sort in descending order.

On Fri, Apr 12, 2019, 6:56 PM Mayur Bagul <mayur...@gmail.com> wrote:
Hello coders,

I'm here to understand line of code which im unable to understand how it work.
i'm looking forward to get help from you.

code is given below :


from django.shortcuts import render

from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)




- in the code above i'm looking forward to know about bold and highlighted part how it work ?

--
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...@googlegroups.com.
On Fri, Apr 12, 2019, 6:56 PM Mayur Bagul <mayur...@gmail.com> wrote:
Hello coders,

I'm here to understand line of code which im unable to understand how it work.
i'm looking forward to get help from you.

code is given below :


from django.shortcuts import render

from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)




- in the code above i'm looking forward to know about bold and highlighted part how it work ?

--
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...@googlegroups.com.

Test Bot

unread,
Apr 13, 2019, 3:58:39 AM4/13/19
to django...@googlegroups.com
That is because 

Question.objects.order_by("-pub_date")

would give you a list of all records so you can simply apply slicing without the use of . operator.
For understanding purpose, you can break down your code as

result = Question.objects.order_by("-pub_date")
latest_question_list = result[:5]

This is a feature provided by Python itself.
Would suggest going through the Django docs about ORM in case you have not.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Mayur Bagul

unread,
Apr 14, 2019, 3:01:14 AM4/14/19
to Django users
Thank a lot for your valuable information on asked topic. My double is clear now.
Thanks to you and community.
Reply all
Reply to author
Forward
0 new messages