简体   繁体   English

如何使用Datatables在Django中制作更快的大数据(5,000,000)没有django-datatables-view No Rest Framework

[英]How to make faster large data (5,000,000) in Django with Datatables No django-datatables-view No Rest Framework

I want to take large data (examples 5 000,000) with datatables in Django .But Proccessing is slowly (Page Load is 12 seconds).Users login to webpage. 我想在Django中使用数据表来获取大数据(示例5 000,000)。但是,处理过程缓慢(页面加载为12秒)。用户登录到网页。 Then Click to Test .html. 然后单击以测试.html。 Backend procces run to test_json in Views. 后端procces在Views中运行test_json。 Views call taking db data with to_dict_json. 视图使用to_dict_json调用db数据。 On Models.py. 在Models.py上。 This method in Views return Json response objects. Views中的此方法返回Json响应对象。 How to make faster large data in Django with datatables 如何使用数据表在Django中制作更快的大数据

Models.py Models.py

class SysTest(models.Model):

class Meta:
    db_table = 'sysTest'

id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Tip = models.ForeignKey(SysRefTip, on_delete=models.CASCADE)
Kod = models.CharField(_("Kod"), max_length=20, blank=False, null=False)
Aciklama1 = models.CharField(_("Açıklama 1"), max_length=100,blank=True, null=True)
Aciklama2 = models.CharField(_("Açıklama 2"), max_length=100,blank=True, null=True)
GrupKod = models.CharField(_("Grup Kodu"), max_length=20,blank=True, null=True)
SayKod = models.FloatField(_("Sayısal Değer"),blank=True, null=True)

def to_dict_json(self):

    return{
        'Id':self.id,
        'Kod':self.Kod,
        'Aciklama1':self.Aciklama1,
        'Aciklama2':self.Aciklama2,
        'GrupKod':self.GrupKod,
        'SayKod':self.SayKod,
    }

View.py View.py

def test_json(request):
    persons  = SysTest.objects.all()
    data = [person.to_dict_json() for person in persons ]
    response = {'data': data}
   return JsonResponse(response,safe=False,content_type="application/json")

Urls.py Urls.py

from django.urls import path, include
from Referans.Views import testView
from django.conf.urls.i18n import i18n_patterns
app_name ="Test"

urlpatterns = [
    path('', testView.test_listele , name="test"),
    path('test/json/', testView.test_json, name='test_json'),

]

test.html 的test.html

            <div class="card-body">

                <table id="myTable2" class="table datatable-responsive-control-right">
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>Kod</th>
                        <th>Açıklama1</th>
                        <th>Açıklama2</th>
                        <th>SayKod</th>
                        <th>GrupKod</th>
                    </tr>
                    </thead>
                </table>





                </div>

            {% block js%}
            <script>

            $(document).ready( function () {
            var t0 = performance.now();
            degerleriGetir('test/json')
            var t1 = performance.now();

            });

            function degerleriGetir(data){

                table =$('#myTable2').DataTable({
                        Serverside:true,
                        Processing: true,
                        orderClasses: false,
                        bSortClasses:false,
                        bDeferRender: true,
                        scrollCollapse: true,
                        scrollY: 500,
                        stateSave: true,
                        scroller: {
                            loadingIndicator: true
                        },            
                        ajax:{
                        url:('test/json'),
                        processing:true,
                        },
                        destroy:true,
                        columns: [
                        {'data': 'Id'},
                        {'data': 'Kod'},
                        {'data': 'Aciklama1'},
                        {'data': 'Aciklama2'},
                        {'data': 'GrupKod'},
                        {'data': 'SayKod'}  
                        ]
                    });
            };

            </script>

            {% endblock js  %}

Asyncronous loading is made with ajax request, for example. 例如,使用ajax请求进行异步加载。 The javascript controls which pages the user wants to see, making a request to django "view_url+?page=X". javascript控制用户想要查看的页面,向django“view_url +?page = X”发出请求。 Then, get the info (usually in json) and loads the content into html. 然后,获取信息(通常在json中)并将内容加载到html中。 I never used DataTable, so i can't tell you exactly what you have to do. 我从未使用过DataTable,所以我无法准确地告诉你你需要做什么。 What I am telling you is what you can do to achieve this asynchronous loading. 我告诉你的是你可以做些什么来实现这种异步加载。 Other option is paginating normally, loading the page each time for each page (so much easier to implement by hand). 其他选项通常是分页,每次为每个页面加载页面(这样更容易手动实现)。 Take a look to django ListView model to make your implementation easier and faster! 看看django ListView模型,让您的实现更轻松,更快捷!

Good luck! 祝好运!

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

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