Converting Flask-SQLAlchemy to JSON With Flask-Marshmallow

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
here you want to say anything from pretty-pretty here in today's video I'll be showing you how to use flask marshmallow to convert your secret alchemy models to Jason very easily so if you use sequel alchemy then you know it can be a little tricky to convert the data in your table in the database table to Jason even though a lot of times that's what you want to do directly oftentimes if you want to do this you have to write your own code that goes through and reads all of the columns in your table and then converts them to a Python dictionary and list combination and then finally you can justify those objects or those data types excuse me and then you'll get Jason output but flask marshmallow makes it very easy for you to do this in somewhat of an automatic way and almost a magical way as well so make sure you understand how marshmallow works a little bit before you try this otherwise this won't make any sense at all and it will seem like complete magic so to demonstrate this what I'm going to do is I'm going to create a couple of sequin models add some data into the database then I'm going to return that data by passing two marshmallows so I can see it as Jason data so the very first thing I need to do is I'm going to instantiate Flass equal alchemy so DB passed in the app and I'm going to create a couple of models for this so I'll create one called user so DB model and the user is just going to be a username so DB column it's going to be an integer primary key is going to be true and then I'll have a name for this so DB column DB string is what I want let's say 50 characters long and then I'll create a second class that will have things that belongs to users so something that could belong to users let's say I don't know rewards so DB model so I'll change this to reward make it a Singler so DB model so I'll have the same primary key so integer primary key is true then I'll have a name for the rewards so let's call this reward name it's going to be a string as well so DB touch string let's make this 250 characters and then this is going to belong to a cert user so user ID so DB column DB integer if I can spell integer correctly and then it's going to be foreign keyed to my user ID and then finally I'll create a relationship so rewards or excuse me I want users and users will be actually no user so I'm thinking back where it's user will be DP relationship and I'll call this user or well it's referencing the user table and then the back reference that I'm going to have on user is rewards so that's where I'm in support rewards so now that I have that I can actually go ahead and add data into the database so I'll do that so I'll start a Python from what is this main import DB DB creates all so that creates the tables in my database and then I'll also imports user and reward so I'll just create two users and three wards that should cover everything so one is going to be the first user so user name is going to be user one very simple name two is going to be user two and then I'll add those to the database so let's see at all I'll pass and one and two and then what I'm going to do I'll come at this first then what I'm going to do is I'll create three wards so let's say first second and third so first is going to be reward and the name is going to be reward one and then the user is going to be one and I should be using reward name not just name so let me go ahead and do that again second will be another reward ward name reward - and I'll also give this to user 1 and then third is another reward reward name World War 3 and this time the user will be - and I just have to commit the session because they get automatically at it so now if I look at the database select star from a user I see two users select star from reward I see three wards okay so I have everything that I want in my database so now what I want to do is I'm going to create a very simple route so app routes slash index return justanna Phi so I needs to import JSON if I just saw an if I and in let's call this in progress so of course this isn't is this isn't exactly what I want so let me run the app and just show you so if I can spell justanna Phi writes then it will work so if I go to the index you see a JSON object just saying in progress so that's what I have right now and what I want to do is I basically want you query for let's say a user and return that user has Jason so first let me get a user so let's say one user and I'll just do user dot query first so this gives me the first two user in the database so I can't simply say one user and then pass it like this because I'll get an error message let's see what the error messages yeah says user one is not Jason serializable so that's where flask marshmallow comes in so what I need to do is I need to install flask marshmallow and marshmallow sequa alchemy so let me stop my app pip install flask - marshmallow I already have it so it won't install again unless well I spelled it wrong so it's gonna complain but flask - marshmallow so marsh mallow and then I need to install Marsh mellow - sequel alchemy as well so make sure you saw those two things otherwise it won't work I have both already so I don't need to get them again so I'll start at my app again and now what I'll do is I will say from flask on the shore underscore marsh mallow import Marsh mellow there now instantiate that after I have instantiate the database for sequel kameez so marshmallow and I'll pass in the app so now that I have that all I need to do is create a schema for both a user in the reward and creating the schema is actually pretty easy I don't have to go through and create the attributes directly I can just pass in my sequel alchemy models and it will be used to create a schema automatically so the first email will be user schema and I need to import ma model schema so as you can see this is kind of similar to how flashy guaca me works and then I'll create a subclass called meta and this will allow me to simply set the model to be equal to user and I'll do the exact same thing for reward so reward schema and I won't be using the reward schema but just to show you this is how it works class well actually it gets used implicitly so the model is going to be equal to reward okay so now that I have that now as you can imagine I just need to dump the user object and then I can see it as JSON so to do that what I'll do is I'll take this one user and I'm going to create a new variable called output and this is what I'm going to pass to the justanna Phi's so output and I'll call this user so output is going to be I'm going to instantiate the user schema so user underscore schema let's call that and ch8 user schema and then that will allow me to do this so user schema I'm going to serialize so I'm going to use dump I'll pass in the actual object which is one user and then because this is a marshmallow object I can either get the data or the ears in this particular case I just want the data so I'll type in data like that and then what this does this is going to convert my user object one that is not jason serializable and it's going to convert it to something that is a mixture of Python dictionaries and lists and those are jason serializable so save that and if I go here I now see if I look at the raw data and zoom in a bit this is exactly what I'm looking for so I have the ID for the user I have the name for the user and I have the rewards for the user one and two and this works well for one user if I want to show all the users and I have to make very minor changes for this schema I have to tell it that I'm expecting this to be many of the user schema so I need to set me needs 'true and then this user query first I'll change this to all to get all the users in the database and I'll just keep the rest of the code the same I'll change this to be users so it makes a little more sense users and then if I run this again and look at the raw data I now see the two users I have in the database so user 1 a user 2 and then we can see the rewards that each has so user one has rewards 1 and 2 and user 2 has reward 3 so as you can see that's very simple to convert your sequel alchemy objects into JSON data using this schema through flask marshmallow so it's kind of magical how it works but if you understand how mark all works then it's not quite magic and it didn't take that many lines of code I didn't have to type out that much and I was able to output my sequel alchemy models as Jason so this works great for any API that you're building and you want to return the contents of the database directly to the user this works pretty well it saves a ton of time when you're writing code because you don't have to go through and kind of customize how each field will be handled in the JSON output and said you can have flask marshmallow I'll handle it for you so that's it for this video if you have any questions you can of course leave a comment down below and I'll get to them if you like this video please give me a thumbs up and if you haven't subscribed to my channel already please subscribe so thank you for watching this video and I will talk to you next time
Info
Channel: Pretty Printed
Views: 46,429
Rating: undefined out of 5
Keywords: flask-sqlalchemy json, flask-marshmallow json, convert sqlalchemy to json
Id: kRNXKzfYrPU
Channel Id: undefined
Length: 11min 51sec (711 seconds)
Published: Thu Dec 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.