Seeing queries produced by Django ORM

In order to achieve this I use a package called Django-extensions

Once installed if I run ./manage.py shell_plus --print-sql I get a nice IPython shell prompt and any ORM queries I run I get an output of the sql.

        
Python 3.5.1 (default, Jun 12 2017, 22:57:56) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from youstayuk.application.models import *

In [2]: Application.objects.all()
Out[2]: SELECT "application_application"."id",
       "application_application"."living_in_uk",
       "application_application"."legal_representative",
       "application_application"."elegible_q",
       "application_application"."country_id",
       "application_application"."user_id",
       "application_application"."name_changed",
       "application_application"."name_changed_why",
       "application_application"."mailing_address_q",
       "application_application"."gender_id",
       "application_application"."relationship_status_id",
       "application_application"."primary_nationality",
       "application_application"."country_of_birth",
       "application_application"."date_of_birth",
       "application_application"."place_of_birth",
       "application_application"."secondary_nationality_q",
       "application_application"."secondary_nationality_still_q",
       "application_application"."secondary_nationality",
       "application_application"."secondary_nationality_date_held_from",
       "application_application"."home_office_reference_q",
       "application_application"."home_office_reference_number", "app


Execution time: 0.008289s [Database: default]

<QuerySet [<Application: 2>, <Application: 1>, <Application: 4>, <Application: 5>, <Application: 6>, <Application: 7>, <Application: 8>, <Application: 10>, <Application: 13>, <Application: 14>, <Application: 15>]>

In [3]: Application.objects.values('id')
Out[3]: SELECT "application_application"."id"
  FROM "application_application"
 LIMIT 21


Execution time: 0.002252s [Database: default]

<QuerySet [{'id': 2}, {'id': 1}, {'id': 4}, {'id': 5}, {'id': 6}, {'id': 7}, {'id': 8}, {'id': 10}, {'id': 13}, {'id': 14}, {'id': 15}]>
    


    

Tags

Return to blog