4

I'm exploring https://github.com/shymonk/django-datatable, it looks nice but Im wondering if it will query the whole data or query the paginated data. This information is highly needed to determine performance.

I would like to know in Django, is there a way to see what is the underlying queries is being executed ?

Im using Django==1.11.7

Axil
  • 3,606
  • 10
  • 62
  • 136
  • Django is lazy query and still lazy after got data. Like i did with datatable, the search function (in search bar) will re-use the data object to filter again – Nam Nguyễn Dec 20 '17 at 03:54
  • Thanks, are you saying with datatable is loads all data ? not the paginated ones ? – Axil Dec 20 '17 at 03:55
  • what is a good way to solve this ? do a separate search then right ? – Axil Dec 20 '17 at 03:56
  • def get_initial_queryset(self): just define the STANDER query, each page/search will add an option of filter to this query ~> each time, datatable reload, it will query again. You can check it in: def filter_queryset(self, qs): Let print like: print("QS - raw:", qs.query) and you will see it – Nam Nguyễn Dec 20 '17 at 07:57
  • Possible duplicate of [How can I see the raw SQL queries Django is running?](https://stackoverflow.com/questions/1074212/how-can-i-see-the-raw-sql-queries-django-is-running) – cezar Dec 20 '17 at 08:22
  • cezar, thank you. this is what im looking for! – Axil Dec 20 '17 at 08:23

2 Answers2

4

If you want to see the actual SQL generated by Django's ORM, you can print out the .query attribute of a queryset. Here's an example with an app called home and model called Question that I generated in the Django shell:

 $ python manage.py shell
(InteractiveConsole)
>>> from home.models import Question
>>> q = Question.objects.all()
>>> print(q.query)
SELECT "home_question"."id", "home_question"."date_added", "home_question"."question", "home_question"."number_of_answers" FROM "home_question"

Looking at the methods of django-datatable, it looks like the filtered and paginated views are based on filtering the results of get_queryset, which returns model.objects.all():

def get_queryset(self):
    model = TableDataMap.get_model(self.token)
    if model is None:
        return None
    return model.objects.all()
briancaffey
  • 2,339
  • 6
  • 34
  • 62
  • 2
    Documentation can be found at https://docs.djangoproject.com/en/dev/faq/models/#how-can-i-see-the-raw-sql-queries-django-is-running – Stuart Dines Dec 20 '17 at 04:39
  • thank you. as datatable is my interest of investigation, how did you know of that function get_queryset ? does it mean it returns all ? and then let javascript to do the filtered result ? – Axil Dec 20 '17 at 04:40
  • @Axil the `FeedDataView` class subclasses [BaseListView](https://ccbv.co.uk/projects/Django/1.4/django.views.generic.list/BaseListView/), so the `get_queryset` method here overrides the one it inherits. But in this case I think it does simply return all objects. – briancaffey Dec 20 '17 at 04:55
  • @StuartDines thanks, I cant get that working though https://imgur.com/a/gKr3Q DEBUG = True in settings.py is – Axil Dec 20 '17 at 05:04
2

Another way to see all the queries that have been performed.

python manage.py shell
>> from django.db import connection
>> from app.models import SomeModel
>> obj = SomeModel.objects.first()
>> connection.queries
>> <Returns a list of all the queries that have been executed in the session upto this point>

This also allows you to see how many queries have been performed

xssChauhan
  • 2,728
  • 2
  • 25
  • 36