Create Django Models

The first step of creating a database is designing a database structure. In Django, the database design is written in the file. In this lesson, we'll explain how to edit


Django models are defined as Python classes. When you run the startapp command, is created.

By default, 'from django.db import models' is already written at the top in This line of code imports a module called models, which will be the basis of the custom models you are designing.

To make a new model, you need to add a sentence to create a new class. The basic syntax for creating a class for a new model is shown in the main figure.

Create Django Models: Edit

There are four parts that you need to customize.

1. Model name

You can use any name (except for reserved words), but it should clearly represent the model that you are designing. One model becomes one data table in the database. You need to write in PascalCase because a Django model is defined as a Python class.

2. Field name

You can also use any name (except for reserved words) representing each model field (data table). Use snake_case for this part.

3. Field type

A field type defines the data type that each field can store. Django provides a list of field types, and you need to select one from the list. For example, CharField (Character Field) can store small- to large-sized strings. The list of field types will be explained later. A field type is written in PascalCase.

4. Field option

There are options you can add in each field. The field options give additional rules for the field. For example, you can allow the field to be blank using the blank option. Unlike the field type, the field option is written in snake_case.


Let's create a new app for this chapter and edit the file. This chapter will guide you to create a database for employee learning programs.

1. Situation

HR skill development team is making a database to manage employees' digital skill learning courses. The team wants to manage the following information:

  • which employees should sign which learning courses
  • which employees should be prioritized for digital skill development

2. Create a new app "employee_learning"

Run the command below to create a new app under the same project created in the previous chapter.

Command Line - INPUT
python startapp employee_learning

You can confirm that a new directory named employee_learning has been created under the project directory.

3. Register the new app in

When you create a new app, you always need to add it in the file so that Django can recognize the newly created directory as a new app.

Edit the file by adding the yellow line, as shown below.


4. Edit the file

Edit the file under the employee_learning app directory. As the first step, create an Employee model with one data field.

The four points below are the parts you need to customize in the basic syntax.

  • Model name: Employee
  • Field name: name
  • Field type: CharField
  • Field option: max_length
from django.db import models

class Employee(models.Model):