In this video I'm showing how I'm creating a Django's model that handles posts for social sharing. I'm showing also how I'm saving the posts in the database using the created Django's model.
Category
🦄
CreativityTranscript
00:00In this video, I'll show how to create a Django model and store data within it.
00:10My Django model is defined in the Posts app.
00:16I use the Models.py file within the app to store the models' fields.
00:31And this is my Django model for social posts.
00:40This model extends the BaseModel class, which provides the core functionality needed to
00:52define database models.
00:57The ID field is an auto-incrementing primary key field, which is automatically included.
01:09This field is a foreign key linking to the custom user model.
01:21This means if this user is deleted, all its posts will be deleted too, and this user will
01:34be used as the owner of the post.
01:39This field is used to store the post text and this the post image.
01:49Next, this field will contain the current date and time when the post is created.
02:02This will contain the table name in the database, and this will return a piece of the post text
02:18in the admin interface to represent the post.
02:25To create the database table for this model, I need to use this li command.
02:43And this.
02:49My table is already created.
02:54To create a post in my view file, I will call the posts model and will set the post data.
03:18Finally, I will save.
03:22Let's see a real example.
03:26Now, I will create a post that should be saved in the database table.
03:52And the post was created as expected.