【问题标题】:Jquery datatables plugin for django用于 django 的 Jquery 数据表插件
【发布时间】:2015-01-04 05:30:06
【问题描述】:

我是 django 的新手。我在我的 django 应用程序中使用 jquery 数据表插件。这些数据表适用于从我的视图发送的小数据集。我有一个 django 模型,它有 65k 条记录和 5 列。当我试图在 jquery 数据表中显示这些记录时,呈现的页面暂时变得无响应并且页面正在正确加载。此外,排序、搜索、分页功能在合理的时间内运行良好。即使我在数据表中显示 65k 个条目,我也希望看到页面响应。有没有办法做到这一点?或者处理大型数据集的最佳解决方案是什么?请建议我

我知道这是因为我在从服务器加载 65k 记录后尝试在客户端格式化数据表。我也用谷歌搜索了n知道服务器端处理将是处理这个问题的最佳方式。任何人请建议我如何在 django 中进行服务器端处理。

现在,我的代码如下:

Inventory.html 的一部分:

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

</div>

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:

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

    });
});

URLs.py:

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

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})

请建议我如何修改它以处理大型数据集。

【问题讨论】:

  • 不要使用 Jquery datatables plugin 使用 django modelforms 和惰性分页
  • @madzohan,你能推荐 django modelforms 和惰性分页的文档吗?

标签: javascript jquery python django datatable


【解决方案1】:

当您这样做时,所有记录都会加载到缓存中。您需要使用迭代。 也许这就是你想要的。

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

或更高级的

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()

【讨论】:

  • 您的观点是关于服务器端内存的,但对我来说,使用 dbname.objects.all() 或 queryset_iterator() 没有区别,因为我在客户端面临问题。我的视图功能能够在合理的时间内将大数据发布到客户端。但是在客户端 jquery 数据表无法处理这么大的数据。
【解决方案2】:

您可以简单地为此使用页面加载器。您可以将页面加载器设置为将数据表正确呈现到页面中所花费的时间。

【讨论】:

    猜你喜欢
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    相关资源
    最近更新 更多