How to Create Model in Django Application

Posted on


Actually, this article has a relation with several previous articles. More important, article with the main purpose of creating a Django project in How to Create Django Project in Microsoft Windows using Command Line. Also an article with the aim to create a Django application in How to Create a Django Application inside a Django Project in Microsoft Windows. After having a Django project and also a Django application inside of it, the next step will be having a Django model definition inside of it. In the context of Django application, having a Django model is very important. Using Django model, it is possible to interact with the necessary data where it will flow as a content supply for the Django application.

How to Create Model in Django Application

So, this part will show how to create a Django model in sequence or in certain steps :

  1. First of all, in a normal condition and situation of a Django project where the Django application exist, Django model is part of a Django application. In other words, the association of a Django model is closely related to Django application. In order to prove it, by a standard and normal convention, to define model, the Django application need  a file with the name of ‘’. Normally, in the time of creating a the Django application, it will also automatically create a file with the name of ‘’ inside of it. But, if it does not exist, just create a file with the name of ‘’ as a first step inside the Django application folder. Below is an example of the structure :

    Folder PATH listing for volume Windows-SSD
    Volume serial number is CA30-19A4
    │ ├───migrations
    │ │ └───__pycache__
    │ ├───templates
    │ │ └───myapp
    │ └───__pycache__

    In the above structure’s context, just create the file with the name of ‘’ inside the ‘myapp’ folder.

  2. Continue on the previous step, just fill the ‘’ file with the following content as an example of a Django model :

    from django.db import models
    # Create your models here.
    class Employee(models.Model):
        name = models.CharField(max_length=50)
        email = models.CharField(max_length=100)
        address = models.CharField(max_length=250)
        class Meta:
            db_table = 'employee'

    So, in order to create a model, just create a class with a models.Model parameter. In this context, the class have a name of ‘Employee’ for indicating that this is a class for defining Employee object. Furthermore, since the Employee object is a person, it will have a characteristic, property or attribute of ‘name’,’email’ and also ‘address’. For more information about Django model, just look in this link. That link points in to a Django Documentation page about Django model.

  3. Last but not least, just save that file.

Leave a Reply