Django Model Definitions: How They Differ From the Generated Database Schema

Adrienne Domingus
7 min readJul 10, 2020

Models are the way we tell Django what we want our database schema to look like, and are the things through which we interact with rows, or instances, in our database.

One of the benefits of using a framework like Django is that it provides functionality making it easier to create and interact with our data than if we had to do so directly with the database and generate and validate our own SQL statements. This means that there are things that will look different in the database than they do in our code — this is okay! But it’s also interesting to look at some of them to better understand where Postgres (in this example, you may be using a different database engine) and Django diverge, and which is driving what behavior.

Let’s build an example model as we go through this, so that at the end we can take a comprehensive look at the differences between our model definition, the migration file, and the database schema.

from django.db import modelsclass BlogPost(models.Model):
# We'll build out our model definition here!

Primary Keys

By default, models are given an id field as the primary key without needing to define it at all (so nothing to add to our example model definition…

--

--