如何在组中按x显示查询集值?

时间:2015-06-12 10:42:40

标签: python django

如何在组中按x显示查询集值?

  JSONArray stops = new JSONArray();

        for (int index = 0; index < mainList.size(); index += 3) {

            RouteCreator route_mon_fri = mainList.get(index);
            Map<String, List<String>> directionMap = route_mon_fri
                    .getDirectionMap();
            String direction = route_mon_fri.getDirection();
            String route = route_mon_fri.getRoute();

            RouteCreator route_sat = mainList.get(index + 1);
            Map<String, List<String>> directionMap2 = route_sat
                    .getDirectionMap();

            RouteCreator route_son = mainList.get(index + 2);
            Map<String, List<String>> directionMap3 = route_son
                    .getDirectionMap();

            List<String> timeEntries;
            for (Entry<String, List<String>> entry : directionMap.entrySet()) {
                String name = entry.getKey().trim();
                timeEntries = entry.getValue();

                try {
                    JSONObject stop = new JSONObject();

                    JSONObject arrivals = new JSONObject();
                    JSONArray timeArray = new JSONArray(timeEntries);
                    //JSONArray timeArray2 = new JSONArray(timeEntries2);
                    //JSONArray timeArray3 = new JSONArray(timeEntries3);
                    arrivals.put("mon-fri", timeArray);
                    //arrivals.put("sat", timeArray2);
                    //arrivals.put("son", timeArray3);

                    stop.put("arrival_time", arrivals);
                    stop.put("stop_name", name);
                    stops.put(stop);
                    System.out.println(stops.toString(3));

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            List<String> timeEntries2;
            for (Entry<String, List<String>> entry2 : directionMap2.entrySet()) {
                timeEntries2 = entry2.getValue();
            }
            List<String> timeEntries3;
            for (Entry<String, List<String>> entry3 : directionMap3.entrySet()) {
                timeEntries3 = entry3.getValue();

            }


}

3 个答案:

答案 0 :(得分:0)

不知道长度不是问题。 Python可以为您做到这一点,您需要知道的是开始和结束。

slicing the list时,如果结尾大于元素数,则python会将其打印到列表末尾。

示例

>>> a_list = range(12)
>>> no_items_per_loop = 5
>>> end = 5
>>> while a_list[ end - no_items_per_loop : end ]:
...     print a_list[ end - no_items_per_loop : end ]
...     end += no_items_per_loop
... 
[0, 1, 2, 3, 4]
[5, 6, 7, 8, 9]
[10, 11]

答案 1 :(得分:0)

在Python中,切片末尾的位置无关紧要:

a = [0,1]

print a[0:2]
# [0,1]

print a[0:5]
# [0,1]

因此,在您的情况下,您可以跳过列表:

item_count = len(items)
i = 0
while i < item_count:
    print items[i:i+5]
    i += 5

答案 2 :(得分:0)

'在组'中按x显示查询集值 - 此概念称为 PAGINATION

您可以使用 Django Paginator 对查询集进行分页。

  

它管理分页数据 - 即分散在多个数据中的数据   页面,带有“上一个/下一个”链接。这些课程都在   django/core/paginator.py

     

为Paginator提供一个对象列表,以及您想要的项目数   拥有每个页面,它为您提供访问项目的方法   对于每个页面。

from django.core.paginator import Paginator

qs = Item.objects.all()
p = Paginator(qs, 5) # 5 is the no. of elements to be displayed each time

# To get total no of pages i.e. no. of times result have to be displayed
total_pages = p.num_pages

# print page 1
for item in p.page(1).object_list:
    print item  

# print page 2
for item in p.page(2).object_list:
    print item  

您甚至可以将打印对象的这个过程转换为函数,然后只传递一个paginator对象和一个页面号。

如下所示:

def display_queryset_values(paginator_object, page_no=1):
    for item in paginator_object.page(page_no).object_list:
        print item  

display_queryset_values(p, 1) #Displays 1st 5 objects 
display_queryset_values(p, 2) #Displays next 5 objects

示例示例(摘自Django Docs):

>>> from django.core.paginator import Paginator
>>> objects = ['john', 'paul', 'george', 'ringo']
>>> p = Paginator(objects, 2)

>>> p.count
4
>>> p.num_pages
2
>>> p.page_range
[1, 2]

>>> page1 = p.page(1)
>>> page1
<Page 1 of 2>
>>> page1.object_list
['john', 'paul']

>>> page2 = p.page(2)
>>> page2.object_list
['george', 'ringo']
>>> page2.has_next()
False
>>> page2.has_previous()
True
>>> page2.has_other_pages()
True
>>> page2.next_page_number()
Traceback (most recent call last):
...
EmptyPage: That page contains no results

>>> p.page(0)
Traceback (most recent call last):
...
EmptyPage: That page number is less than 1
>>> p.page(3)
Traceback (most recent call last):
...
EmptyPage: That page contains no results