Models in Code
Let's learn about the structure and syntax of writing models in Django.
We'll cover the following...
We'll cover the following...
Structure of model class
As we mentioned in the previous lesson, we have model classes that inherit from Django’s built-in models.Model
class.
Let’s look at an example of a blog post to see the structure of a Model class:
Let’s break down and analyze the code lines one by one:
First, we imported some useful libraries.
class Post(models.Model): – this line defines our model (it is an object).
-
classis a special keyword that we use in Python to define classes. -
Postis the name of our model. -
models.Modelmeans that thePostis a Django model and that it is derived from theModelclass.
Now, we have to define the properties of the post: title, ...