Integrating Python with a database is a fundamental part of systems development with Python and Django. This allows data to be stored in a structured and efficient way, making it easier to manipulate and retrieve information when needed. In this chapter, we'll explore how to create tables in a database using Python and Django.

First, it's important to understand that Django uses an Object-Relational Mapping (ORM) to interact with the database. This means that you can work with database data as if they were Python objects, without having to write SQL queries. Django supports a variety of databases, including PostgreSQL, MySQL, SQLite, and more.

To create a table in Django, you need to define a model. A model is a Python representation of a database table. It contains fields and behaviors of the information you want to store. Each field is represented by an instance of a Field class, which determines the type of data the field can contain and how it behaves.

For example, let's create a table to store information about books. First, you need to create a new Django application (if you don't already have one) using the command python manage.py startapp app_name. Then, inside your new application's models.py file, you can define the model:

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=100)
    publicacao_data = models.DateField()

This is a very simple table, containing just three fields: title, author, and publication date. Note that we are using different types of fields for each piece of information, according to the type of data we want to store.

Once you've defined the model, you need to tell Django to create the table in the database. This is done through migrations. Migrations are like a controlled version of your database, which Django uses to create, alter, and drop tables as needed.

To create a migration for your Book model, you can use the command python manage.py makemigrations app_name. This will generate a migration file, which contains the code needed to create the table in the database. To apply the migration and effectively create the table, you can use the command python manage.py migrate.

Once you've created the table, you can start inserting, changing, and retrieving data using the Django ORM. For example, to insert a new book, you can do:

book = Book(title="The Lord of the Rings", author="J.R.R. Tolkien", publicacao_data="1954-07-29")
book.save()

This is just a basic example of how to create tables and manipulate data in Django. The framework offers many advanced features such as relationships between tables, data validation, complex queries and much more. Throughout this course, we'll explore these features in detail and learn how to use them to build robust and efficient systems with Python and Django.

Now answer the exercise about the content:

What is the role of Object-Relational Mapping (ORM) in Django?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Python Database Integration: Inserting Data 148

Next page of the Free Ebook:

Python Database Integration: Inserting Data

Estimated reading time: 4 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks