Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creation of Models | Models
Django: Build Your First Website

Creation of ModelsCreation of Models

In this chapter, we will create models for the application.

Models are created in the models.py file.


This is a Django model that describes objects of the Note type. You can think of a model as a template or blueprint for how data will be stored in the database for Note objects. Let's take a closer look at the fields of this model:

AppFolder \ models.py
  1. title: This field is a CharField with a maximum length of 60 characters. It means that the title of each note will be stored as a string, and its length cannot exceed 60 characters;
  2. content: This field is a TextField, which allows for storing more text than a regular CharField. It is designed to store a large amount of textual information, such as the content of a note;
  3. created: This field indicates the time the note record was created. auto_now_add=True means that this field automatically gets the current time when a new Note object is created and remains fixed at that time;
  4. __str__() method: This method returns a string that will be used to represent a Note object in a human-readable form. In this case, it simply returns a string containing the title of the note.

So, in this model, we define how data should be stored for Note objects, including their title, content, and creation time. This model can be used to create a table in the database that will store information about notes.

Example in Django project

Thanks to our file system, you can check the Django code right in the chapter. You can also review your code as it visually demonstrates how properly written code looks and in which files this code should be.

arrowfolder
admin.py

admin.py

folder/
folder

notes

folder

my_notes

folder

migrations

admin.py

admin.py

apps.py

apps.py

models.py

models.py

tests.py

tests.py

urls.py

urls.py

views.py

views.py

folder

notes

asgi.py

asgi.py

settings.py

settings.py

urls.py

urls.py

wsgi.py

wsgi.py

manage.py

manage.py

requirements.txt

requirements.txt

Everything was clear?

Section 3. Chapter 3
course content

Course Content

Django: Build Your First Website

Creation of ModelsCreation of Models

In this chapter, we will create models for the application.

Models are created in the models.py file.


This is a Django model that describes objects of the Note type. You can think of a model as a template or blueprint for how data will be stored in the database for Note objects. Let's take a closer look at the fields of this model:

AppFolder \ models.py
  1. title: This field is a CharField with a maximum length of 60 characters. It means that the title of each note will be stored as a string, and its length cannot exceed 60 characters;
  2. content: This field is a TextField, which allows for storing more text than a regular CharField. It is designed to store a large amount of textual information, such as the content of a note;
  3. created: This field indicates the time the note record was created. auto_now_add=True means that this field automatically gets the current time when a new Note object is created and remains fixed at that time;
  4. __str__() method: This method returns a string that will be used to represent a Note object in a human-readable form. In this case, it simply returns a string containing the title of the note.

So, in this model, we define how data should be stored for Note objects, including their title, content, and creation time. This model can be used to create a table in the database that will store information about notes.

Example in Django project

Thanks to our file system, you can check the Django code right in the chapter. You can also review your code as it visually demonstrates how properly written code looks and in which files this code should be.

arrowfolder
admin.py

admin.py

folder/
folder

notes

folder

my_notes

folder

migrations

admin.py

admin.py

apps.py

apps.py

models.py

models.py

tests.py

tests.py

urls.py

urls.py

views.py

views.py

folder

notes

asgi.py

asgi.py

settings.py

settings.py

urls.py

urls.py

wsgi.py

wsgi.py

manage.py

manage.py

requirements.txt

requirements.txt

Everything was clear?

Section 3. Chapter 3
some-alt