简体   繁体   English

Django的jQuery Datatables插件

[英]Jquery datatables plugin for django

I am newbie to django. 我是django的新手。 I am using jquery datatable plugins in my django application. 我在我的django应用程序中使用jquery datatable插件。 These datatables are working fine for the small datasets sent from my view. 这些数据表对于从我的视图发送的小型数据集工作正常。 I have a django model which is having 65k records with 5 columns. 我有一个django模型,它具有5列65k记录。 when I am trying to show these records in jquery datatables the rendered page becoming unresponsive for a moment and the page is loading correctly. 当我尝试在jquery数据表中显示这些记录时,呈现的页面暂时变得无响应,并且页面正在正确加载。 Also sorting, searching, pagination features are working fine with reasonable to amount of time. 排序,搜索,分页功能在合理的时间内也可以正常工作。 I want to see the page responsive even when I am showing 65k entries in datatables. 我想看到页面响应,即使我在数据表中显示65k条目也是如此。 Is there any way to do this? 有什么办法吗? or what will be the best solution to handle large datasets? 或处理大型数据集的最佳解决方案是什么? Pls suggest me 请建议我

I came to know that this is because I am trying to format datatables on client side after loading 65k records from the server. 我知道这是因为从服务器加载65k记录后,我试图在客户端格式化数据表。 Also I googled n knew that server side processing will be the best way to handle this. 我还用谷歌搜索n知道服务器端处理将是处理此问题的最佳方法。 Any one pls suggest me how to do server side processing in django. 任何人都建议我如何在Django中进行服务器端处理。

Now, my code is as follows: 现在,我的代码如下:

part of Inventory.html: Inventory.html的一部分:

<div class="box-body table-responsive" id='postinfo'>

</div>

InventoryOutputAlldata.html: InventoryOutputAlldata.html:

{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script type="text/javascript">
    $(function(){
     $('#example1').dataTable({
        });

      });
    </script>
</head>

        <table id="example1" class="table table-bordered table-hover">
              <thead>
                <tr>
                    <th>Device</th>
                    <th>Device Type</th>
                    <th>Mac Address</th>
                    <th>IP Address</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody >
            <form name="trform" method="post">
              {% for key,value in data.items %}

                <tr  class="trselected" onclick="trclick(this)"> 

                        <td>{{ value.0 }}</td>
                        <td>{{ value.1 }}</td>
                        <td>{{ value.2 }}</td>
                        <td>{{ value.3 }}</td>
                        <td>{{ value.4 }}</td>
                  </tr>
              {% endfor %}
             </form>
              </tbody>
              <tfoot>
                <tr>
                    <th>Device</th>
                    <th>Device Type</th>
                    <th>Mac Address</th>
                    <th>IP Address</th>
                    <th>Status</th>                                           
                </tr>
            </tfoot>
        </table>

</html>

JS: JS:

$(function(){
    var data = new Object();
    data['showdata'] = 'all';
    data["csrfmiddlewaretoken"] = $("input[name='csrfmiddlewaretoken']").val();
    $( "#postinfo" ).load( "/alldata/", data, function( response, status, xhr )
    {

    });
});

URLs.py: URLs.py:

urlpatterns = patterns('',
    url(r'^inventory/$', TemplateView.as_view(template_name='inventory.html')),
    url(r'^alldata/$', 'NetworkInventory.nmap_integration.alldata'),
)

views.py: views.py:

def alldata(request):
    postedInfo = request.POST
    count = 0
    dataDict = {}

    dbData = nmap.objects.all()
    if 'showdata' in postedInfo and postedInfo['showdata'] == 'all':
        for data in dbData:
            count += 1
            dataDict[count] = []
            dataDict[count].append(data.device)
            dataDict[count].append(data.devicetype)
            dataDict[count].append(data.macaddress)
            dataDict[count].append(data.ipaddress)
            dataDict[count].append(data.status)

    else:
        return HttpResponse('Improper Behaviour')

    return render_to_response('inventoryOutputAlldata.html',{'data': dataDict})

Please suggest me how can i modify this to work with large datasets. 请建议我如何修改它以使用大型数据集。

When you do it that way all of the records are loaded into cache. 当您这样做时,所有记录都将加载到缓存中。 You need to use iterate. 您需要使用迭代。 Perhaps this is what you want. 也许这就是您想要的。

car_set = Car.objects.all()
for car in car_set.iterator():
    #Do something

Or more advanced 或更高级

djangosnippets Try this djangosnippets尝试一下

import gc

def queryset_iterator(queryset, chunksize=1000):
    '''''
    Iterate over a Django Queryset ordered by the primary key

    This method loads a maximum of chunksize (default: 1000) rows in it's
    memory at the same time while django normally would load all rows in it's
    memory. Using the iterator() method only causes it to not preload all the
    classes.

    Note that the implementation of the iterator does not support ordered query sets.
    '''
    pk = 0
    last_pk = queryset.order_by('-pk')[0].pk
    queryset = queryset.order_by('pk')
    while pk < last_pk:
        for row in queryset.filter(pk__gt=pk)[:chunksize]:
            pk = row.pk
            yield row
        gc.collect()

You can simply use a page loader for this. 您可以为此简单地使用页面加载器。 You can set the page loader to the time taken for your datatable rendered correctly into the page. 您可以将页面加载器设置为将数据表正确呈现到页面中所花费的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM