Models in Code
Explore how to structure Django model classes with different field types and establish relationships. Understand how to migrate the database automatically and set up the Django admin interface to manage your data. This lesson helps you create models, register them, and use superuser accounts for database interaction.
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, ...