How to check for SQL queries generated in your Django project.
Django is a high-level framework that enables rapid development of secure and maintainable websites. It takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. It allows you to connect with databases like SQLite, Postgres, and MySQL.
Django uses the Django ORM to send data between the database and model. It relieves you of the stress of writing SQL and reduces errors. For beginners, a database can be explained as a structured set of data that allows you to retrieve information at any point you need it. On the other hand, a query can be defined as a request for data results from your database or for an action on the data.
Once you've created your data models in Django, it allows you to create, retrieve, update, and delete objects in your models. Let's get into a more complicated concept: the QuerySet. A QuerySet represents a collection of objects from your database. It can have zero, one, or many filters. Filters narrow down the query results based on the given parameters. In SQL terms, a QuerySet equates to a SELECT statement, and a filter is a limiting clause such as WHERE or LIMIT. For example, a QuerySet that retrieves all instances of a model from a database can be written as q = ModelName.objects.all()
, which is similar to SELECT * FROM TableName
in SQL.
Object-Relational Mapping (ORM) serves as a middleman that helps move data in the native language of a programming language. Our main focus is to show you how you can see all of those SQL Queries in your Django project and help you understand how you can perform complicated queries from your database.
To check all SQL queries in your Django project, you can install the Django Debug Toolbar package using the following command in your terminal:
pipenv install django-debug-toolbar
Then add debug_toolbar
to your INSTALLED_APPS
:
INSTALLED_APPS = [
'debug_toolbar',
]
In your middleware, add the DebugToolbarMiddleware
:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
]
Make sure to add the IP addresses of your local machine to the INTERNAL_IPS
list:
INTERNAL_IPS = ['127.0.0.1']
In the urls.py
in your root folder, generate the URLs for the panels and views:
if settings.DEBUG:
import debug_toolbar
urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)),
path('admin/', admin.site.urls),
path('', include('{nameofyourproject}.urls')),
# ... your other URLs ...
]
After starting your project, you should see a widget at the edge of your page that gives details about HISTORY, VERSIONS, CPU TIME, HEADERS, REQUEST, SQL, CACHE, STATICFILES, and SIGNALS.
Remember that this should only be used in your development environment and not in production, as it could impede performance.
Thank you for reading. Please let me know in the comments what you think about this content and give your suggestions for what's next.
You can also follow me on my social media channels to get my latest insights on Twitter
Stay tuned for more updates on my blog, and don't forget to subscribe to my Youtube