Django Shell Create a New DB Record - Python Web Development

Опубликовано: 23 Октябрь 2024
на канале: codevev
1,131
30

#Python #Shorts

In the last video we created a database but currently it's empty. Let's see how we can add a new record to our Task table.

We're going to run "python manage.py shell", which will bring up an interactive shell that will allow us to interact with out app from the command line using Python code.

First, let's import our model. "from coolapp.models import Task".
Then, we can create a new instance of a Task and give it a name, the other two field will be set to their defaults.
And lastly, we can call .save on the instance that we created and Django will save it to the database as a new row in the Tasks table.

In the next video, we will see how we can query what we just inserted.
-------------------------------------------------------------------------------------
Links:
Blog: https://evgenyurubkov.com
Twitter:   / evgenyurubkov  
Instagram:   / evgenyurubkov  
Youtube:    / @codevev