Manipulating Data With Django Migrations
What’s Under the Hood?

Django migrations are Python files that help you add and change things in your database tables to reflect changes in your Django models. To understand how Django migrations help you work with data, it may be helpful to understand the underlying structures we’re working with.

What’s a database table?

If you’ve laid eyes on a spreadsheet before, you’re already most of the way to understanding a database table. In a relational database — for example, a PostgreSQL database — you can expect to see data organized into columns and rows. A relational database table may have a set number of columns and any number of rows.

In Django, each model is its own table. For example, here’s a Django model:

from django.db import models

class Lunch(models.Model):

left_side = models.CharField(max_length=100, null=True)

center = models.CharField(max_length=100, null=True)

right_side = models.CharField(max_length=100, null=True)

Each field is a column, and each row is a Django object instance of that model. Here’s a representation of a database table for the Django model, Lunch , above. In the database, its name would be lunch_table .

A table representing lunch_table.

The model Lunch has three fields: left_side , center , and right-side . One instance of a Lunch object would have Fork for the left_side , Plate for the center , and Spoon for the right_side . Django automatically adds an id field if you don’t specify a primary key.

If you wanted to change the name of your Lunch model, you’d do so in your models.py code. For example, change Lunch to Dinner , and then run python manage.py makemigrations . You’ll see:

python manage.py makemigrations

Did you rename the backend.Lunch model to Dinner? [y/N] y

Migrations for 'backend':

backend/migrations/0003_auto_20200922_2331.py

- Rename model Lunch to Dinner

Django automatically generates the appropriate migration files. The relevant line of the generated migrations file in this case would look like:

migrations.RenameModel(old_name="Lunch", new_name="Dinner"),

This operation would rename our Lunch model to Dinner while keeping everything else the same. But what if you also wanted to change the structure of the database table itself and its schema? Maybe you’d also like to make sure that existing data ends up in the right place on your Dinner table.

Let’s explore how to turn our Lunch model into a Dinner model that looks like this:

from django.db import models

class Dinner(models.Model):

top_left = models.CharField(max_length=100, null=True)

top_center = models.CharField(max_length=100, null=True)

top_right = models.CharField(max_length=100, null=True)

bottom_left = models.CharField(max_length=100, null=True)

bottom_center = models.CharField(max_length=100, null=True)

bottom_right = models.CharField(max_length=100, null=True)

It’s database table would look like this:
['Victoria Drake']
['Programming', 'Software Development', 'Data Science', 'Technology', 'Learning To Code']