How to Create a Django MySQL Database with Django Models

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is tony teaches tech i'm tony and in this video i'm going to show you how to set up a mysql database for django and connected to a django model and pretty much how they work together with each other as far as migrations and all that stuff is concerned so by the end of this video you should have a pretty good understanding of that let me show you uh what the goal is the overall goal of this videos and then we'll get right into it so right here is a website that i have it's a wordpress detector so uh when you come in here this is a django website when you come in here search for a website it'll say if it's running wordpress or not so google.com is not a wordpress website tony teaches.tech is a wordpress website so basically the goal is to log in a database a new database record every time somebody searches for a domain name i want to log what they search for and whether or not it's wordpress so very simple concept let's see how we do that with django so we'll minimize this for now i'm already logged into my server here and this is the root of my django project and i have my django app here the the detector app and if we look in here real quick we'll see that you see some of these familiar files such as your views your urls and all that stuff so i'm going to assume that you have some knowledge with django you have something set up already that you're going to use and then you can model it after what we talked about in this video so the first thing we want to do is do a pseudo apt get update just to make sure we're looking at the latest packages and then we want to do a i'm just going to copy this on my notes over here sudo apt-get install sorry about that uninstall uh maria database server and lib maria database client-dev and that's the reason we need to do those two packages is because that's the the database type that we're going to be using you could do vanilla mysql as well but my preference is maria database so that's going to take up 191 megabytes we will i'm not sure how quick this i think this is a pretty quick install we'll let that go through and install uh but after that's done we're also gonna have to install a um a python package that allows python to communicate with the server the mysql server so looks like this is finishing up here i am using virtual environments for my python i highly recommend the same i'll have a video about that if you're interested in that but basically we're going to have to activate that in virtual environment and then install the package that i'm talking about so let me activate my virtual environment i'll do that by sourcing python 3 8 bin activate that will probably be something different for you and then i'm going to use the pip package manager to install my sql client python package so yeah that i think this one's pretty quick as well so now we have the ability to have a mysql database let's create the database next so we can log into my sql with sudo mysql my sql and then this is the mysql command prompt now what i'm going to do is create a user for that the database first so create user calling my user this he's going to be able to operate on localhost and identified by my password obviously you want to pick a stronger password than that but that's what we're going to use for this video and we're going to grant all privileges on all databases and tables to that user okay and and again this is just for the sake of demonstration this is not something that i recommend that you do in production websites but um that's what i'm going to give my user the full privileges and then finally i'm going to create a database called oh it is underscore database okay and as far as interacting with mysql directly is concerned that's all we have to do everything from this point forward we're going to leverage django models in order to allow django to create our tables and insert into our tables and update our tables and all that stuff so we can get out of here go back to our terminal session here a regular command prompt and let's tell django that we just created that database so we can do that again we're in here in our the root of our django project typically this is in a subdirectory in a something called settings.pi and in here there's a database section somewhere here it is databases and by default they're using a locally a local database on on your file system that's just a file which is sql lite 3 version 3. we want to get rid of these two lines and replace it with some information that we just uh um created our database through mysql so what is that what do i mean by that so in here and i already have this uh on the clipboard here you wanna add the following lines based on the information that you created your database with so if you're using mysql or maria database that engine will be this and then the name of my database as you saw was oh it is underscore database the user that we created his name was or his or her name was oh it is the password was my password the host is localhost this which means this current server that we're on and then the port 3306 which is the default port from my sql so um go ahead and fill out your default database information here in your settings.pi file and we'll go ahead and save that and now that django knows where to interact with the database and it has the credentials to do that let's start talking about django models so um in my django app here in my detector django app let's go into that directory there is a models.pi file right here okay and that's where we're going to be defining the model for our database so let's open that up and we'll get rid of this comment here we'll keep the import from djangodb import models i'm going to copy and paste some code in here and i'll explain it along the way but again for your purposes this will be obviously what you want to do with your website so basically we're defining a class the name of the class is search log it's of type model okay and you can think of this as um a table right the class corresponds to a table and then these three lines here correspond to uh i guess ro not rows columns in your database right so different pieces of information that we want to track in the database so like i was saying we want to track the url that the user searches for whether or not it's wordpress and then some extra content some metadata here the time the date and time that they search for that so the url value is of type character field and these are all standard django types model types so you can look at the documentation to figure out what the options are for you so um character field max length 255 characters so 255 characters for the url that they type in up to that amount um is wordpress i i is wp is wp this is a boolean field so it's going to be either true or false and the default is false okay and then the search time is of type date time field and it's going to default to now as the the value for that okay so this is basically defining our table structure okay so let's save that and now that we have that now that we've created that model let's um let's move that information to the database so that's a two-step process okay so let's go back to the root of our django project and we there's in your in the root of your django project you have your manage.pi file so let's do python manage.pi make migrations and what this is going to do and i'll show you no changes detected that's interesting [Music] let me try make migrations detector o d-e-t-e-c-t-o-r detector oh that's right okay so i forgot to do this um but no no big deal here if we go back into our settings file i forgot that we have to install that app so um that was in the oh it is directory and then settings.pi you need to make sure that you have as part of your installed apps the name of your django app so for me like i said the name of my django app is detector as you saw by the directory that we we navigated into um right here that's that's the the django app not the project the app that i had created so now when we do uh python manage.pi make migrations it'll look in that folder it'll see that we defined a model and then it'll create the model that we defined so that is literally at this directory you can see what that looks like and this is auto-generated code so there's not much to do in here but now that this exists this this migration exists for detector we can apply those changes to the database essentially and we do that with python manage.pi migrate okay so that's going to go to the it's in addition to some other things here as you can see it's going to create the table on our mysql database and we can see that actually happening if we go to our mysql database again we use the oh it is database and then we show tables and remember this was a couple minutes ago this was not a database that didn't exist so django created all these tables with the the migrate command and then here is our table so select star from detector that's the name of the app and then the uh the table search log and it's an empty set i forget describe we do describe detector underscore search log so you'll see that we have these are the fields the id field uh just an auto incrementing id the url ver varchar 255 is w is wp that's the boolean field it's just going to be a tiny int and then the search time date time okay so let's let's now that we have our database table django knows about it our database is all set up let's access let's work with our model within our python code within the context of django so um as a reminder let's well let's go to our let's go to reviews.pi file so we'll go into the detector section here of our django app and then open up the views.pi file and one of my views is the index page and if basically without getting too complicated here if if there's a get request happening that means that the user has searched for a value and what we want to do is basically at that point save that information that the user search for to the database so right here we have we're getting the url from the get request and we're defining whether or not it's wordpress based on a call to scan site so what we can do here right below here is add a couple lines of code i will explain what this means so um well actually i guess we first have to import that so bear with me for a second until we get all caught up here with the code and then i will explain it so okay so right here first thing i'm going to do is import from the models.pi file in this directory the one that we were working with earlier i import search log so that's the name of the class that we defined and we'll show you that again just in case you forgot we're importing this class okay and then with that down here when the user makes a search we're going to instantiate a that class an object of that class type we're going to pass in the url that the user searched for and the result of whether or not the uh the website is wordpress or not this i wrote this function so i know it returns a boolean a true or false and is wp so we're going to create that object and then save that object and when we do the object.save that's going to insert a row into our database you know all done behind the scenes and save that permanently to our database so that's how like once you get the infrastructure working for django models it's really really easy to be able to interact with the database this is just this is just an insert you can do a lot more you can do updates deletes all that stuff but just for the sake of demonstration let's start with something basic like this okay so we'll save that and um i think i think we're good to go uh i i'm not 100 sure about this but i will do a um a restart of my uh whole django site here and i'm using emperor if you if you want to if you want to know how i host my production websites for django i'll have a video about that but i'm not 100 sure if this is necessary or not to make those changes apply to the real website but just for just so nothing you know just so i don't mess up anything while we're recording this video okay so um we'll test this out live real time let's open up our mysql database again we want to use the otis database and then select all from the search log so there's nothing in there right now let's do let's go back to the home page and let's do a search for what's another website um yahoo.com detect wordpress it says wordpress is not a yahoo is not a wordpress website what we care about more than that is whether or not it's saved to the database and it did not save to the database so uh what could the problem be here let me see if i can figure this out in real time um select all from detectors so that should oh i i went to the wrong website we have to go to my test.oz.com which um let me go to a guest window test.oh it is dot com no what am i saying test.oh it dot is bear with me we'll get there okay let's try that again yahoo.com detect wordpress it's not a wordpress website let's look at our mysql database select star there we go so i had the wrong url um the id the url that the user searched for is it wordpress no it's not that's the zero and then the time that it's cert that they search for that uh let's do one more tony teaches dot tech detect wordpress waiting waiting waiting not sure why it's taking so long took too long to spawn so tony teaches.tech uh let's just do um is that a problem with my website is it online tonyteaches.tech it is online let's try it again tony teaches tech okay something weird so it worked that time let's see what our database looks like uh there we go we got another record in our database tony teaches tech it is a wordpress website and that's the time that we search for it um guys this was a a basic introduction in django models i hope it was helpful to you if you have any questions about it let me know down in the comments below i'll do my best to help you out i got plenty of other videos on django tutorials over here um so if you're interested in this topic uh check those out as well please subscribe to this channel for more videos like this from me in the future and if you do i'll see you in the next [Music] one
Info
Channel: Tony Teaches Tech
Views: 46,174
Rating: undefined out of 5
Keywords: django mysql, how to connect mysql database to django, connect mysql with django, how to connect mysql database in django, how to setup mysql database in django, how to use mysql database in django, mysql and django, django database, django database settings, django add database, django get data from database, django models, django model create
Id: IiUYyZo2gTk
Channel Id: undefined
Length: 17min 39sec (1059 seconds)
Published: Wed Jul 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.