Introduction
Data is at the core of most web applications. Unless we're talking about a very simple application such as a calculator, in most cases we need to store data, process it, and display it to the user on a page. Since most operations in user-facing web applications involve data, there is a need to store data in places that are secure, easily accessible, and readily available. This is where databases come in handy. Imagine a library operational before the advent of computers. The librarian would have to maintain records of book inventories, records of book lending, returns from students, and so on. All of these would have been maintained in physical records. The librarian, while carrying out their day-to-day activities, would modify these records for each operation, for example, when lending a book to someone or when the book was returned.
Today, we have databases to help us with such administrative tasks. A database looks like a spreadsheet or an Excel sheet containing records, with each table consisting of multiple rows and columns. An application can have many such tables. Here is an example table of a book inventory in a library:
In the preceding table, we can see that there are columns with details about various attributes of the books in the library, while the rows contain entries for each book. To manage a library, there can be many such tables working together as a system. For example, along with an inventory, we may have other tables such as student information, book lending records, and so on. Databases are built with the same logic, where software applications can easily manage data.
In the previous chapter, we had a brief introduction to Django and its use in developing web applications. Then we learned about the Model-View-Template (MVT) concept. Later, we created a Django project and started the Django development server. We also had a brief discussion about Django's views, URLs, and templates.
In this chapter, we will start by learning about the types of databases and a few basic database operations using SQL. After that, we will move on to the concept of models and migrations in Django, which assist in faster development by providing a layer of abstraction to facilitate database operations using Python objects.