Reader small image

You're reading from  Django in Production

Product typeBook
Published inApr 2024
Reading LevelIntermediate
PublisherPackt
ISBN-139781804610480
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Arghya Saha
Arghya Saha
author image
Arghya Saha

Arghya (argo) Saha, is a software developer with 8+ years of experience and has been working with Django since 2015. Apart from Django, he is proficient in JavaScript, ReactJS, Node.js, Postgres, AWS, and several other technologies. He has worked with multiple start-ups, such as Postman and HealthifyMe, among others, to build applications at scale. He currently works at Abnormal Security as a senior Site Reliability Engineer to explore his passion in the infrastructure domain. In his spare time, he writes tech blogs. He is also an adventurous person who has done multiple Himalayan treks and is an endurance athlete with multiple marathons and triathlons under his belt.
Read more about Arghya Saha

Right arrow

Exploring Django ORM, Models, and Migrations

In Chapter 1, we saw how to create a Django project and integrate Views into the project. All the responses in the Views were static and we didn’t see how to implement any code to get dynamic responses using databases.

In this chapter, we shall see how to integrate a PostgreSQL database into our Django project and the support Django provides out of the box to work with models and databases. If you have worked with relational database management systems (RDBMSs) before, you will know how painful it is to perform migrations whenever the schema is updated. However, Django provides migration support out of the box, which abstracts all the intricacies under two management commands.

After that, we’ll learn a few best practices while working with Django models and how to check for performance optimizations with large database tables. Finally, we’ll check out the newest feature that was introduced in Django 4.1: Async...

Technical requirements

In this chapter, we’ll work with the Django model and database. To work with the code in this chapter, you will need to have Django installed on your system with the project structure discussed in the previous chapter. You should be comfortable in creating Django apps and connecting Views to the URLs, as we have learned previously.

Apart from these, you should have the following in your skillset:

  • You should understand the basics of RDBMSs and how databases work at a high level. You should also be familiar with the concepts of normalization and denormalization of databases.
  • You should understand the basics of Django ORM and have worked with Django models before (you may have followed and understood the Django Girls tutorial mentioned in the previous chapter).
  • You should understand the concepts surrounding SQL and have PostgreSQL installed on your system or have a stable internet connection to use a remote PostgreSQL server (https://www...

Setting up PostgreSQL with a Django project

Django integrates SQLite3 for initial development purposes out of the box, which helps developers get started with their projects without the hassle of initial database setup. In our case, we used SQLite3 in Chapter 1. However, it is a file-based database that is not recommended to be used in production since it cannot scale.

Instead, we shall use PostgreSQL as our choice of database to work with Django throughout this book. Since it is always recommended to keep your local development environment as close to the production environment as possible, we shall set up PostgreSQL for our local development.

Important note

You can use other supported databases such as MySQL, MariaDB, and Oracle (https://docs.djangoproject.com/en/stable/ref/databases/#databases) by just making a small change in the settings file. But please remember that not all databases support all the ORM functionalities, so if you are using another database, check the...

Using models and Django ORM

For any organization, data is one of the most important foundational elements in Django, and the models.py file is used to structure how data is stored in the database. A well-architected system would have a strong Django model foundation, where the developer has spent enough time and done proper research before creating the database schema.

One of the most common questions developers have while designing the schema is whether to perform normalization or denormalization. The answer is always “it depends.” It depends on multiple factors, such as the data access pattern and the performance bottleneck we are trying to solve. So, as a rule of thumb, it is always preferred to start with a normalized database schema design and then, as we find different use cases, try to solve the problem by introducing caching or indexes, and then finally perform denormalization.

Important note

We won’t focus on the basics of how to work with Django...

Understanding the crux of Django migrations

Migration is one of the most dreaded words for an engineer, be it migrating an old framework to a new framework, migrating data from an old system to a newer system, or in our case, migrating database schema-related changes. In fact, in a lot of early-stage startups, developers use NoSQL primarily because the business is in such a nascent stage; developers don’t want to make database migrations every other day as the business requirements have to adapt to the new database schema requirements.

However, Django solves the database schema migration issue out of the box. In this section, we’ll learn how Django handles migration under the hood and how we can work with Django migrations like a pro to solve any challenge thrown at us.

Demystifying migration management commands

Whenever we ask any developer how to perform migration in Django, the most common answer we get is that we can perform migrations by running the following...

Exploring best practices for working with models and ORM

Django’s official documentation is a very detailed guide on how to work with models and ORM, and it helps you write complicated queries using ORM. But when you create real-world applications, it is more than just writing good queries; there is a lot of nitty-gritty that you should know or get to know after making mistakes yourself. In this section, we are going to learn about all those small tricks that can help us work better with Django models and ORM.

Use base models

Abstract base models are one of the most preferred model inheritances in Django. We should create common base models whenever we find duplicates of the model attributes. One of the most useful base models that I have found is TimeStampedBaseModel, which has created_at and updated_at fields. Using this base model to create any model will automatically add the created at and updated at timestamp field; for example, whenever someone creates a DemoModel...

Learning about performance optimization

In today’s modern web world, any API response between 0.1 to 1 second is considered a good overall response time. In the whole request-response cycle, the database often becomes the bottleneck and we see slower responses, primarily due to bad queries or bad database design decisions.

Therefore, in this section, we’ll learn how to check for performance optimizations in the Django ORM and database layer. In the previous sections, we learned how to work with models and design database schemas using different normalization techniques, but here, our primary goal is to learn how to use Django ORM efficiently to gain performance optimizations.

One of the first techniques we should understand to gain insight into our application performance is using the explain feature of the database query.

Demystifying performance using explain and analyze

As we develop applications in today’s world, a lot of people might frown upon...

Exploring Django Async ORM

Django Async ORM was introduced in Django 4.1 to help developers take advantage of native support of asynchronous behavior in Python. When we use asynchronous Views or code in Django, we cannot use the native synchronous ORM. Using synchronous Django ORM would mean we are calling blocking synchronous code from asynchronous code, which will block the event loop. Django has a way to stop this, and we’ll see a SynchronousOnlyOperation error thrown by Django during such an operation.

Read more

If you are not familiar with async-await support in Python, please check out the official documentation to understand how Python natively supports async-await: https://docs.python.org/3/library/asyncio-task.html.

We should use Django Async ORM with Django Async view/code. The asynchronous query API would give the same response as the original synchronous Django ORM query API. The only difference is how we write the syntax of the query.

For example, let...

Summary

In this chapter, we learned a lot of concepts regarding how to work with models and databases. First, we learned how to configure our Django project with ElephantSQL, which enabled us to work with PostgreSQL on our local system without the hassle of setting up a PostgreSQL server. Then, we learned about the basic concepts of ORM and how we can use normalization in Django models, along with the model’s inheritance concept.

Migration is a crucial part of any application, and we saw how Django eases the pain of database schema migration. We learned the tricks of using Django migration in our regular development cycle and the best practices we should follow while working with it. Django ORM is one of the pioneers of how frameworks should handle database interaction. After, we covered the best practices that we can follow with Django ORM before looking at a couple of performance optimization tricks that developers can use to build a scalable application.

Django 4.1...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Django in Production
Published in: Apr 2024Publisher: PacktISBN-13: 9781804610480
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Arghya Saha

Arghya (argo) Saha, is a software developer with 8+ years of experience and has been working with Django since 2015. Apart from Django, he is proficient in JavaScript, ReactJS, Node.js, Postgres, AWS, and several other technologies. He has worked with multiple start-ups, such as Postman and HealthifyMe, among others, to build applications at scale. He currently works at Abnormal Security as a senior Site Reliability Engineer to explore his passion in the infrastructure domain. In his spare time, he writes tech blogs. He is also an adventurous person who has done multiple Himalayan treks and is an endurance athlete with multiple marathons and triathlons under his belt.
Read more about Arghya Saha