Use Models in django


let's see how to create an application in django project and, how to make and use Models for storing the data required in the project.


I try to develop the serverside with python django. in this blog, I’ll introduce how to create new application in django, and how to make and use Models for storing the data rquired in the application.

if you want to know how to install django and configure django, see the previous blog posts.

Create django Application

django has a big unit called Project and a small unit called Application. one project can have many applications. in this blog, we’re going to develop a blog with django. execute the django command below to create blog application.

# virtualenv venv
# source venv/bin/activate
# pip install -r requirements.txt
# django-admin --version
# 2.2
python startapp blog

after creating, you can see the folder structure like below.

|-- django_exercise
|   |--
|   |--
|   |--
|   |--
|-- blog
|   |--
|   |--
|   |--
|   |--
|   |--
|   |--

new files in the application folder do the following.

  • Admin page configurations file that django basically provides.
  • Application main file
  • Application Models file
  • test file
  • Application Views file

there are other files that are used in django.

  • Application url management file
  • input form file
  • Model mixin option file
  • Application constant file
  • decorator file
  • test data factory file
  • Vies and Models helper function file
  • custom model manager file
  • custom signal file
  • Views mixin file

I have no idea what they are. we’ll study one by one after, so we just check these files are existed and move on.

we’ve created django application, so we need to register the application to django project. open django_exercise/ and modify it like below to register the application.


Create Models

let’s see how to create Models to develop the blog site. open blog/ and add the codes below.

from django.db import models
from django.utils import timezone

class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=100)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    published_at = models.DateTimeField(blank = True, null = True)

    def publish(self):
        self.published_at =

    def __str__(self):
        return self.title

let’s see details.

  • Post has author, title, content, created_at, updated_at, published_at fields
  • we use ForeignKey function for author to referrer User model of auth application that django basically provides.(auth.User: AppName.Model)
  • we use CharField function to store fixed-length string type for title field. we set max_length option to store 100 length string.
  • content is the blog post content field created by TextField to store not fixed-length text
  • created_at is the blog post create date created by DateTieField function to store date and time. we used auto_now_add option to store create time when the data create.
  • updated_at will be the blog post modification date field. auto_now option makes it store update time when the data updated.
  • published_at is the blog post publish date field.

I didn’t explain about blank = True, null = True because I think I need to explain separately.

  • blank: this option is related to the validation. when we use form.is_valid() to validate input data, this options will be used. the data can be blank.
  • null: this is related to the database. this option allows the data can be null.(nullable)

this model has publish, __str__ functions.

  • publish: this is for the publish feature in the blog post servie. when we publish the blog post by using this function, the blog post data’s published_at will be updated.
  • __str__: this is the standard python class method that returns human-readable string.

Create Table By Models

let’s see how to create database table by Models that we created.

Create Migration File

first, we need to create migration file for creating database table by our Models. execute the django command below to create the migration file

python makemigrations blog

after executing, you can see the result like below.

create migration by django models

and, also you can see the migration folder and files are created.

|-- django_exercise
|   |--
|   |--
|   |--
|   |--
|-- blog
|   |-- migrations
|   |   |--
|   |   |--
|   |--
|   |--
|   |--
|   |--
|   |--
|   |--

Create Table

execute the django command to create the table by the migration that is created by Models.

python migrate blog

after executing, you can see the result like below.

create database table by django models migration file

if you use database tool, you can see the table we created.

check the database table created by migration file


Done! we’ve seen how to create django Models and create the database table by Models. now, we’re ready to store data for developing. let’s design DB and make Models and Migration files belong to it!

Buy me a coffeeBuy me a coffee