How to Use Flask-SQLAlchemy With Flask Blueprints

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm going to show you how to use flat sql alchemy with flask blueprints so a lot of people have asked me about this recently and i thought i'd create a video about it so here what i have set up is a virtual environment that already has flask and flash sql alchemy installed and what i want to do is i want to start by creating an app directory so make directory and i'll call this my project and then inside of this my project directory i need to create a dunder init file so underscore underscore init underscore underscore dot pi and then inside of this done during it this is where i'll create the create app function so from flask import flask and then i'll create the function so create app and then i'll return this app so next what i want to do is i want to make sure i can run this in my development environment so i need to export flask underscore app and this will be equal to my project and also i can change the environment to development so i can get debug mode working so if i do flask run that should work just make sure you're in the base project directory not in your app directory so if you can get this far you know you have that set up correctly and now what i want to do is i want to start to bring in a model and then i'll explain what you have to do to make it work so i'll create a file called models and this should actually be in my project so models.pi and in here what i'm going to do is i'm going to create a user model so db.model and then the id will be an integer and the primary key will be true and then i'll just have a name column so i'm not going to do much with this so we'll just make that 50. and notice that i'm using db here but i don't have it yet right and you should also know that when you use any extension in flask you have to initialize it so typically in my videos i do something like db equals sql alchemy and then app and the reason why i do that in the videos is to cut down on writing code like i just want to show you the main point of the video so i do it this way but when you're building a real app you probably don't want to do it this way instead what you want to do is you want to use this so db.init app and then pass in the app object right so i can get rid of that but in both cases you should notice that i'm using db there in db here but i haven't created it yet so the main question is where do i put this so basically i'm going to have something like this from flask sql alchemy import sql alchemy just like that and then i'll do bb equals sql alchemy just like this so i need to have this somewhere but where should i put it and this is where everyone gets tripped up so this is really the point of the video once you have this part down everything else becomes pretty easy so can i put it here so i see a lot of tutorials they'll put it here right i see tutorials where they put it here right so that's another option but in both of these cases it doesn't work very well and the main issue that you're going to run into is the idea of imports and circular imports so with the dunder init file the problem is you're going to have your models file importing from your dunder init so this is kind of like the entry point of your app but you have other parts of your app kind of depending on it or it should be the other way around it should be your dunder init imports everything from your app and then runs it you shouldn't have parts of your app importing from dunder init so this is not gonna work with models it's kind of a similar thing you can use the models and have them imported in other files like routes which i'll show you shortly but you don't want to do that because you're going to kind of tie this db with any particular model and if you want to create more models and more files then where do you put it so you don't want to do it here either so where should you put it you should create another file for this so i like to call this extensions so touch my project and then extension stop pi and now i have this file and i'll put this here so because i only have one extension i'm just putting classique alchemy here but if i had other extensions i can put them here as well and i'll show you that later in the video when i use flask migrate so now if i go to dunder knit what i can do is i can say from dot extensions import db and this dot just means basically on the same level as this file so underneath and extensions are on the same level so that's why i use one dot and then i can do the exact same thing in the models dot pi and everything works right so let's see if i can create this database so first i need to add the configuration for the database so i'll say sql alchemy database uri is equal to sqlite and then we'll call this db.sqlite3 like i always do and what i need to do is i need to go into my python repo and import some things to get this to work so typically if you use like the really basic flask app structure then this is really easy you just import db and then do db.create all so let me try that so what i'll do is i will say from my project import db and then i'll do db dot create all and the reason why i'm able to do this is because i'm importing db and dunder in it you should know that it's from extensions but i can import it from dunder in it because i just did that i'll import it from extensions in a moment but we see i get this error no application found either work inside a review function or push an application context that's a typical error when you're using the application factory so what i want to do instead is i want to say from my project import db or let's say from my project dot extensions to keep it consistent so from my project dot extensions import db and then what i also want to do is i want to import that create app function so from my project import create app and then finally for some reason you need to import all of your models so i'll say from my project dot models import star so if i had more than one model then everything gets imported so now what i need to do to create the database is i can do db.create all just like before but i need to pass in an app and the app is going to be whatever create app returns so create app and i'm calling it so i need the parentheses and then i get this warning about the track modifications but it created the database for me so now if i open up my sqlite viewer so my project and then db.sqli3 and i do tables i see users there if i do schema i see it has the id and the name working so now let me create a route and a blueprint for that route so you can see how this works if you're using a blueprint so what i'll do here is i'll create a new file called route stop pi and in here i'll create a blueprint for like main so from flask imports blueprints and i'll say main so i'm calling this blueprint main it's going to be blueprints the name is main and then i need to pass in dunder name and then i can use this like any other route so just on the index well how about this i'll do user and then name so i'll use this to create new users so user say create user so there are no name collisions and then i need to pass in the name here and what i want to do is i want to use my user object which i'll import in a moment name is equal to name and then db.session.ad and of course i'll import db in a moment so db.session.ad user so let me actually assign this to a variable so user equals user name and then db.session.commit and then i'll just return something like created user right so i need to import both the user class and the db object so i can do that i'll say from dot extensions it's on the same level import db and it's actually not on the same level so i'll just move it into this directory here now that should be fine and then i need to import the models.pi so from models.pi import user and now i should be able to use this so what i'll do is i'll do flask run and do i get any errors it doesn't look like it let me just add this configuration value to turn this warning off so app config and then we'll just set that to false okay so now i should be able to go to uh localhost 5000 slash user and i'll add a username anthony and let's see if i get an error here so i do get an issue with this but that's pretty easy to solve the issue is i've created the blueprint here but i haven't connected it to my app yet so to do that i'll go back to my dunder init and i'll say from dot routes import main and then inside of my create app i can do app dot register blueprints main so now if i go back to the same place and refresh this i see create a user so i'll just say anthony for one and john is the other one and then let me open up my sqlite database so my project and then db.sqlite3 select star from user and i see anthony and john are there so we know that this is working with the whole blueprint setup okay so this works just fine and if your app isn't that big i think this structure is great so you can put all of your routes inside of this routes file if you have more than one blueprint you can put it here if you want and then likewise with the models if you have multiple models you can put them all here but what if you wanted to have one file per model and one file per blueprint well you can do that as well so i'm going to convert this to a format where i have multiple models so let me start by creating a directory called models so inside of my project we'll name this models and then when i create these directories i need to add dunder and it's inside of them so all of the imports work so models is going to have dunder init and then i'll move my models.pi inside of models and this model is the user model so what i'll do is i'll rename this to user and then i'll create a second model so i'll just copy this and it's going to be pretty similar i'll call this let's say video dot pi and this model will be video and in addition to the name let's just say it has a url as well just to have second model right so i have my two models here one in user.pi and one in video.pi and i have the dunder net so now let's see what happens when i run the app so i get this error message while importing my project an import error was raised so this error message is really annoying because it doesn't tell you much like obviously there's an import error somewhere but where is it and for this you just kind of have to know what you're trying to achieve and all the places you have to look so obviously has something to do with imports so you want to check the imports that you just did so let me go over to these two files so let's take a look at this import first so from dot extensions import db so if you remember what i said earlier this first dot means it's on the same level as the file in question but right here i'm in this models directory there is no extensions on the same level actually up one level so to go up one level i use another dot so dot so dot dot extensions import db i can do the same thing in the video pi right so now let me try running this again and i get another error and once again it doesn't tell me much so i know these are fine so i'll close these this extensions is fine this is a dunder net but now i'm in my route stop hi so if we look here dot extensions is fine because my routes is on the same level as extensions but models is a little bit different so before i just had a file called models but now i have a directory called models and in it i have two files so to get the user i need to do models dot user so i'm going into the models directory which is on the same level as routes and then i'm going inside of user to get the user cloud right so now let's see if this works and it does so if i go back over and create a new user let's say stacy stacy should have been created so let me just confirm this in my database select star from user and i see stacy is there so now i want to create multiple files for my blueprints so what i can do here is i can do a similar thing so i'll create a new directory called routes inside of routes i'll add the dunder nit just like i did for models and then i'll move my route stop pi into the routes directory and then i'll rename this like main all right and then i can have another blueprint called api and for this one i really i'll just do pretty much the same thing so in api i'll just paste everything over and you know how about this how about we just get one user so i can get rid of the db.session and i'll just use user.query.filterbuy name equals anthony and then i'll get the first and then since this is the api i'll return something like this so user and then user.name okay so everything should be fine there i'll rename this api because it's a different blueprint and now if i try to run my app i get an error again of course so the first thing is i need to bring in this new blueprint and register it like i did the main one if i go over to my underneath for the project i need to register the blueprint so app.register blueprint api and of course i need to import that so how do i import it so it's similar to how models work so routes is a directory on the same level as my dunder init so i should be able to do dot routes just fine and then i can do dot main to take me into the main.pi file and then i can do a similar thing for api so from routes.api import api and now in these two files i want to make sure these two imports are okay so i have from.extensions import db so is this extensions on the same level well i'm inside of routes it is not on the same level anymore so i have to go up one directory and then it's going to be on that level so dot dot i can do that in both files and then here the models is models on the same level as my main.pi it doesn't look like it i need to go up one directory into the models directory and then to user so i'll do dot dot there and dot dot there and then i can do something similar if i wanted to and i can import the video model that i'm not using so import video just like that and imagine i had some code to work with video so now let's see if this works and it does so everything appears to be working server is running i'll add one more user will and then i'll open up my database select star from user and i see will is there okay so if that is how you use sql alchemy then this can be all you need but i'll show you how to use this with flask migrate as well so what i'll do is i'll install flask migrate so pip install flask migrates and now what i'll do is i'll go over to my extensions.pi so remember if i'm using extensions i can instantiate them here and from flask migrate import migrate and then i'll just instantiate it so migrate equals migrates and nothing goes inside of it now in my dunder nit for my project for my app i can import migrate from extensions and then i can initialize it down here so migrate.init app you have to pass in both the app and db so both of those are fine and then this should be enough to get it working because setting up flask migrate is pretty simple so what i'll do is i'll remove my database and then i'll try flask db init okay so it created the migrations directory for me perfect and then i'll do flask db migrate and if i go to my directory and go to versions here i see it found my user model and my video model and it's creating the tables for those so now if i do flask db upgrade it should have created my database file so i'll open that up so my project db.sqli3 and then select star from user well actually do i have the tables there yes so we know this is using flask migrate because i have this olympic version table and i also have user and video here and i have nothing in the user table because i just deleted and recreated my database so for one last time i'll go back and i'll create my user anthony and then i'll stop the server open up the database and select star from user and it's there so this really isn't that complicated as you've seen really it's just knowing how to use the imports and really where to put that first db equals sql alchemy where you instantiate it once you get that down everything works so if you still have any questions about this if you want to know how to use it with other extensions just let me know in the comments down below and i'll try to answer
Info
Channel: Pretty Printed
Views: 22,315
Rating: undefined out of 5
Keywords: flask-sqlalchemy, blueprints, create_app, flask, flask extensions
Id: WhwU1-DLeVw
Channel Id: undefined
Length: 20min 9sec (1209 seconds)
Published: Wed Feb 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.