Course Content
Django: Build Your First Website
Django: Build Your First Website
Creation of Models
In this chapter, we will create models for the application.
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:
-
title: This field is a
CharField
with a maximum length of60
characters. It means that the title of each note will be stored as a string, and its length cannot exceed60
characters; -
content: This field is a
TextField
, which allows for storing more text than a regularCharField
. It is designed to store a large amount of textual information, such as the content of a note; -
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; -
__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.
admin.py
notes
my_notes
migrations
admin.py
apps.py
models.py
tests.py
urls.py
views.py
notes
asgi.py
settings.py
urls.py
wsgi.py
manage.py
requirements.txt
Thanks for your feedback!