-2

Currently I have this code that shows me the normal data table but if I have millions of records it becomes very slow, I understand that serverside of datatable can be used but I don't know how I can implement it

list.html


<table class="table table-striped table-hover responsive" style="width:100%" id="buyer">
          <thead>
            <th>#id</th>
            <th>Fecha</th>
            <th>Cod Cliente</th>    
            <th>Cliente</th>
            <th>Dirección</th>
            <th>Comentario</th>
            <th>Tipo Retiro</th>
            <th>Total</th>
            <th>Guias a Generar</th>
          </thead>
          <tbody>
            {% for item in ResulOrdenes %}
            <tr>
              <td>{{item.DOCNUM}}</td>
              <td>{{item.DOC_DATE|date:"Y-m-d"}}</td>
              <td>{{ item.CARDCODE }}</td>
              <td>{{ item.CARDNAME }}</td>
              <td>{{ item.ADDRESS }}</td>
              <td>{{ item.COMMENTS }}</td>
              <td>{{ item.URETIRO }}</td>
              <td>{{ item.DOCTOTAL }}</td>
              <td>{{ item.NGUIAS }}</td>    
            </tr>
            {% endfor %}
          </tbody>
        </table>
    </div>
      


    {% endblock %}

    {% block content_wrapper %}
    <p></p>
    {% endblock content_wrapper %}

    {% block js_page %}

    <script>
    $(document).ready(function() {
      $('.table').DataTable({
        dom: 'Bfrtip',
      });
    </script>

    {% endblock %}

view.py


def Listado_guia(request):
   template_name='app_listguia/lista.html'
   conn = dbapi.connect(..)
   cursorSol2 = conn.cursor()
   sql_command = 'select a."DocNum" as "DOCNUM", a."DocDate" as "DOC_DATE", a."CardCode" as "CARDCODE", a."CardName" as "CARDNAME", a."DocTotal" as "DOCTOTAL", CEILING(a."DocTotal"/20000) as "NGUIAS", a."DocStatus" as "DOCSTATUS", a."Address" as "ADDRESS", a."Address2" as "ADDRESS2", a."Comments" as "COMMENTS",  a."U_Retiro_mercaderia" as "URETIRO" '
        sql_command = sql_command + 'from "'+ connections.databases[ConfigBaseHana]["NAME"] +'"."ODLN" as a '
        
  cursorSol2.execute(sql_command)
  RES_ORDENES = cursorSol2.fetchall()
  cursorSol2.close()
  conn.close()
  dnow = today.strftime("%Y-%m-%d")
  contexto = { "ResulOrdenes":RES_ORDENES, "dnow":dnow }
  return render(request,template_name,contexto) 

urls.py

from django.urls import path
from django.contrib.auth import views as auth_views
from app_guia.views import *

urlpatterns = [
    path('guiaList',Listado_guia,name='urlListGuia_list'),
]

Can you please give me some light on how to proceed? Thanks

4
  • 2
    Where exactly are you getting stuck? Can you be more specific? Have you looked at any tutorials? Those are generally more appropriate than Stack Overflow for such a broad question. Commented Feb 10, 2023 at 17:58
  • You may get some insights from this answer. Otherwise, look at the official documentation and its various examples. There is even some basic PHP code there, to help get you started. Commented Feb 10, 2023 at 17:58
  • Apologies - please ignore the final part of my previous comment. I saw you are asking about Python, but I got stuck on PHP. Anyway, perhaps the other parts may still be helpful. Commented Feb 10, 2023 at 19:49
  • Start with the Django tutorial where you'll learn about Django models from which you can query a database. Commented Feb 13, 2023 at 20:41

1 Answer 1

3
+25

The approach you are using is not suitable for millions of records. First of all, if this is your complete sql, you should stick with normal Django QuerySets and switch your view to use a (generic) class-based view. This gives you a proper paginator, which will reduce the load on the database, django and the resulting html significantly.

If you do not want to have pagination, but rather a continuous list, you need a javascript component to virtually render your table. This way, only a few hundred DOM nodes are present in the clients html, and will be populated from server, on the fly, while scrolling. The term to search for is "virtualized table", an examplatory candidate would be react-window.

On a sidenote: if the code shown is your "real" production code (and not a boiled-down demo version for this post), I highly recommend working through a django tutorial, e.g. here or here, and sticking to the default django way as documented in the djangoproject as much as possible. You will save yourself hours of programming and huge problems with future enhancements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.