Django Part 3: Sqlite3 Database and Migrations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so we have learned how to create a project and run the server how to create a basic app now i want to show you how to set up the database for this application so what i want to introduce now is models.py which is inside our europe application and it's empty for now and django by default says create your models here so if we want to create a model that represents a euro trip from one european country to the other we might define a class let's call it trip so let's create a class we'll call that trip inside here the settings models dot model so we need an origin country we need a destination we need the number of nights and we need the price for that trip so we will need an origin country and this will be equal to models dot character field with a maximum length of 64 characters and we want a destination country so it's exactly the same we'll just change the name of the variable and we want the number of knights so models dot integer field because it's not a character it's rather an integer and we can say the same thing on price okay so we have created our class trip which will serve the purpose of being a blueprint to our database so we don't have to worry about creating table or adding some records or updating them or dropping any of these records because django will take care of all of that and as your application starts to get bigger in size as a result of lots of interactions with the clients for instance you probably want to add changes to your code classes for instance or any other area of your application therefore the databases should also be changed as a natural reflection to that it would be quite tedious if every time you change something in your code you have to reflect these changes in your database files manually and this is the role of migrations right here which has only one file called init.pi and currently it's empty okay so migrations will take care of all what we have discussed previously it detects automatically what are the changes that were made in your file and reflect these changes in your database and how to do that actually before i show you that there is one more configuration step that we need to do in settings.py so inside our installed apps variable right here we need to add in the first line we will add the name of the application which is europe inside a string so europe.apps dot europe config so we will type europe config with a capital c like that and the logic behind this actually is inside our europe application there is a file called apps.py and this simply has one class called europe config and it has one parameter which is appconfig with the name of europe the main name of the application and django does that just to say that i'm aware of your application and the name of this application is europe just the most important thing is that in settings and installed apps you need to insert this line right here the name of the application dot apps dot europe config we might just put europe the e capital like that i will just open my bash terminal i'm inside the folder i will need to enter world tour then in your terminal go ahead and type this with me so we'll type python manage.py followed by a command called make migrations make migrations and what this command does actually that it looks to models.py and it looks to any changes that might happen or happened in that file and automatically will make migrations in other words reflecting the changes in your database so as you can see inside europe folder inside migrations folder django has created this file right here triple zero one underscore initial dot pi this file right here so it has a class of migration with the dependencies and this is basically the code that we have written inside our models okay so we have origin destination knights and price and the same thing here so these are the fields right and notice that django has created id for us i didn't create id in the models but django has created id so each time we will add a record this will be auto incremented so the first trip will have an id of one the second trip automatically will have an id of two and so on origin models dot character field maximum 64 and the rest of the code that we have written in models dot py but let me actually show you what's inside that triple zero one underscore initial dot pi file so still inside my world tour folder i'll type python py sql migrate and the name of the app followed by triple zero one okay take a look at that so we have here begin create model trip and this is very similar to our sql lite code that we have seen in our sqlite 3 course so it starts by creating model trip so to create a database then we create a table called euro underscore trip with an id it's an integer not null which means that it shouldn't be empty primary key which means this is a primary key via which i can relate to other tables or to increment which means that it will be incremented automatically i don't have to add each time an id for the record then origin varchar64 not null knights integer not null price integer not null then followed by commit which means save or commit to these changes and i'm done and origin and destination destination should be here i think it was it's not well presented in um in bash so let me actually show it to you here in the terminal and just we will make this bigger so again let's get inside our world tour and we will type python manage dot pi sql migrate europe triple zero one okay so it's clear here as we said the id the origin we have destination here it was cut here for some reason i'm sorry for that um we have knights which is integer not null price integer and not null so this is sql code that i didn't have to write it was generated for me by django's migration system just simply by looking in what i said i wanted in my database right here in the models right and django did that for me so now i want to apply the migration to the database till now we didn't apply uh the migration so we want to create the table inside the database basically this is what we want to do so the database will be called trip as you can see below here and the table will be called europe trip this is the name that django has given to my table and it will have the attributes that we have set here okay so how we're going to do that well let me just maximize um the terminal so what we'll need to type is python manage.py migrate hit enter fantastic what that did that it has applied all the migrations as you can see here applying content types of admin zero one zero two zero three and so on okay so it has applied old migrations and even the last one applied that was created to us zero zero zero one underscore initial and now my database will have a table that represent the trips via this europe application okay so this concludes the video the next video we are going to see how we can uh work with the shell and how we can add trips to our database so thank you very much and i will see you in the next video
Info
Channel: Bek Brace
Views: 12,191
Rating: undefined out of 5
Keywords:
Id: RzkVbz7Ie44
Channel Id: undefined
Length: 9min 23sec (563 seconds)
Published: Mon Dec 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.