Lets Create an Ecommerce API with Django and Django Rest Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
um what's up guys um it's fate and in this tutorial we're going to be creating a an e-commerce api using jungle it's going to be somehow robust and i'll try my best to make it as beginner friendly as possible um um let's start right away i i made an entity relationship diagram just the basic one or the entities we're going to be working with um here we have book we have products and we are going to have a category that we can reuse over and over again should we have more entities like let's say you want we decide in the next feature to create an article api also so e-commerce api let's start right away um firstly i've used them draw.io to quickly sketch something so let me let me just give you some of the things we are going to do first we will create um create a github repository repo for our project for our django e-commerce api okay and then we now get straight into development so we install jungle storage angle we set up environment okay and we we just viewed yes i came huge so first off okay and let's not right away firstly um i'm trying to and people i'm trying to make sure this video is not so long so today do not be boring so let me go straight to github github github.com if you don't have a github account today as a developer as an aspiring developer it doesn't speak well of the issue and they want to get one it's free so this is my github so i could just come here to this plus let me plus and click new repository so what name should we give you let's say youtube um no no i don't want to show anything you okay youtube e-commerce jungle oh this name is so long e-commerce wow this is boring okay let's see the basic description a full-fledged full-fledged jungle um full-fledged e-commerce api built in jungle on youtube okay great so let's create repository and and voila yeah we have a repository for our project project so first come to your computer where do you want it to be um i have a dev folder and i have a jungle folder um excuse me let me use this youtube dev series jungle i'm gonna create um a new folder yeah so let me even open this folder and assign my terminal my cmd okay so make a new directory mkdir um becomes e-commerce api cd jungle city into it and then you can just set up your environment uh install ppm you should look that up how to install pvm so vpn shell ppm shell to set up an environment in this current folder so um i want to go into the boring details of explaining what an environment is okay i think it's actually to help you package your folder um put the dependencies in order and everything so that to make build and other things like deployment easy for you um later okay so so now we have set up an environment in this in this folder so what we should do now is to install jungle so p pem peep inside environment that's the meaning of people okay people precise this isolated environment so people have installed jungle [Music] yeah sure so as we create um if api's in jungle we use particular framework also i call it primo for microphone jungle rest framework so you're going to see um this tutorial so let's store jungle first uh you have to walk i don't know what's wrong with my internet it's taking taking so long bye bye it's quite good okay good installation succeeded is creating a epi file.lock so great so now we have jungle here so the very first thing you do in jungle projects let me clear the screen or the upon the clutter so the very first thing you do is to create a jungle project so i will do that by doing jungle admin start project what do we call it e-commerce api is that okay okay e-commerce api click enter you already have it if you just dial for those of you using mac or linux it's just list ls so just here okay so now you can see you can see these um e-commerce apis already here so so just cd into it e-commerce api and then from here you can now create an app so juggle is organized based on project and app so several apps can be inside one project so so what we should do now is to create an app inside there you can see a manager py is like your um utility tool for doing a lot of awesome stuff in jungle so let's do python manage dot py startup um [Music] django django um django e-commerce django api [Music] just to designate that this is the app not the projector okay let's click that dude yeah again you see a django api app so now we have an app so the very next thing you want to do is to open this inside your preferred ide um i think the most popular for now is vs code everybody knows that so by typing just code space dot you can open this particular current folder for a directory inside vs4 so click enter and it will fire up vs code okay so now we've got our django projects opened inside vs codes um i really don't want to bore you with all of these details so we have the settings files of course we'll come here just in a moment so we have to register our new app this is the django api app so register your app inside the settings so um okay done so jungle api app config i'm not setting so come to installed apps i usually prefer to put it at it topmost so make my app the very first thing django sees online startup so got to make it um django api app dot apps dot django api config or you can just simply just put the name of the app inside the jungle api it still registers it anyway so um the very first to do now the next thing we'll do is let me come here okay we have created a github repo and then i've created a project we have set up our environment um the next thing we want to do is to create our models um let me see let's create models so come to models these are come to models um let's come to our diagram again we have a book entity uh this one is a particular identity book is a particular data object so that's all of these fields you have id title category um i've decided to make a category also another entity on its own so although you can you can decide differently but i just think it should be an idea so that should i have more entities i can just make them just use it as a foreign key so category um let's go and create so first let's make the category let's make the category model so yeah these are only categories in models that will make models in jungle so class are they oh their name models dot model okay so and then do you have the title is it title category just title so ids auto generator so it house what else the child food there's the chat chat food i'll put a max lens property um let's say yeah actually for a patch of a category let's say um 255 characters i don't know ah okay great so now we're done with category model next thing is to create a book model now let's go after basically book model has an id um book so that will do the same thing class oh coming so let me clean it i don't need a classmate so just need a stronger presentation wait okay so book class book actually this is not alphabetically so just doing it as i feel it's better it's okay so okay try to try to use a chaffey of course who does the chat whereas the field match length for the title of a blue cruise what's let's say 150 characters 50 characters okay i think i prefer to make my visual studio smaller so that i could go faster i don't want this video to be unnecessary long so so category let's do category category is models i told you i'm going to make category a foreign key on all the others so what is that foreign key that's the category that's just up here that's just up here okay category um related name related name is um let's call it um book books yeah let me say books okay and then we we say um we can just say on delete it doesn't take so long delete equals to models cascade um the meaning of this one delete is that if a category is delayed deleted um every book record that associated with a particular category should also be deleted from the database so dynasty in the book is isbn every book as an isbn number isbn astrology isbn it's a 13 digit number so um excuse me i mean whatever's not traffic of course it's going to be a traffic max lens max lane let's say 13 okay then then pages is to tell how many what how many pages does this book have how many pages so mode is on integer so integer field so just make it as simple as possible because i want to make this video as beginner friendly it's possible price i will also use an antigua field for the price also you can use the money period decimal fee nothing so stock so i'm how many of this products of this book do we have currently in in our in our database the stock mode is the tiger also okay and then we're gonna have a description description it's gonna be a summer lengthy test about um about this particular book that we're talking about like so not a reviewer description by us the program the product or the store owner what is the text view is going to be a test you know and then we have an image url image url models dot url huge okay and then um i can put it defaults and put a default as if any is not provided i can put a i don't know let's just leave it for now okay status status means that is this book available what is the boolean i want to make it and the default is that the book is available default true and then date created the did the book was created so i want to make it a dates field i don't need daytime i just need it so auto now auto now add this you say true okay so i can put an ordering i can put the class method now dies by in our class so you say ordering is um by the date it was um by the date it was created so we can just say dates created okay so okay so this is cool you can now make a string representation okay so return choose the title and you choose the title return self dot the title of the book so i'm good now so i don't know let me expand this great we have two models now now let's make the last model that's a product product i want to make this like um first like every other things like um bags shoes mobile phones say we sell mobile phones and so on so many other things so of course now begin with is the product tag it says this imaginary product i like some five digits or five characters that identifies our product uniquely besides the primary key so so mothers thoughts gonna make this a challenge max length max length let's see 10 say 10 okay then usually i like to do something like product name for the price product quantity i don't want to disturb myself so i'm trying to save some time product mode name is char field also max lent is um is a hundred let's say hundred hundred characters category category just like we did for book we are going to do the same thing you could literally just copy it so morris. foreign keys that's the category model we're using for all of them related name letter name is um products products that are under this particular category then on delete is these models dot escape that is if that category is deleted all the products online should be deleted removed from the database also so category uh then we have price prices uh models that i could just copy i could just copy them from book prize thank you price what is that integer huge tiger then the next one is um best one is quantity right here quantity did that mean quantity or stock no yeah yeah literally the same thing so you just use stock stock how many do we have in stock so many do we have in stock so then the next thing we have is um so i'm removing this quantity right now so okay good good now um so image url steros and dategrades so i think these three are common to both of them so let me just copy completely right before if you know that um last matter or died by all right bye please create it i want to i want most recently added to be at the top so okay um i want the product name i want the product name do i want to name my prototype do i want the product name and the product type yes prototype i'm proud of you so i could just do something like this format um self dot i want the product tag and self dot net also great okay so so okay so that's that's all for now so now we have our motors we have our motors now um now we have models created next you want to do is to sort of run for this um project we're going to use the jungle inbuilt sqlite database we are provisioned with see it's easier right here let's see here so so if you were planning to use another database say something roblox like mysql or postgre so you could just add the connection properties here so let me try to run the migrations now all our project motors are here already let me try to run migration so okay so let me do python item manage the py make migrations to run your migration so okay our drain must be a top wall list like make a mistake somewhere or drink oh sorry ordering the least the least sorry okay so it's at least that was very serious okay fight all my ui and make vibrations so we're still doing the normal jungle stuff right now we've never started creating the api so so we'll first make our models lay them out and then we'll now go ahead to make them into an api so okay and you can run your item manager py migrate and then your migrations are all run now great so we get we're being good shape uh the next thing we want to do now is to um set up models next we want to do is to set up our url so force the project level url these e-commerce videos are pushing the url set this up so that will be um parts let me include import include here include and then we have parts you can do something fancy like api version one okay if you have a section one should include to include urls from the app so that would be what's the name of our app again jungle api app dots urls so they include jungle attack urls good you have set up pretty level urls um next you want to do is to now um let me before we go into any other thing i'm going to create a urs folder for the half level also urls of py okay from jungle dot urs import imports parts okay url patterns let me just lay lay an empty pattern so so i'm going to fill in the urls later so the next thing we want to do now we have our models uh in fact register your model in the admin the admin dot p wi-fi so you have to import all of them so from this is where in the current so from dot models imports how many of them do we have category model we have the book model we have the product model so product model so okay so just do simple admin.site register register um email outside the register category okay you can you can do administration start book mingle side of register product studio i'm trying to make this as simple as possible so that every person can easily understand what we're talking about so okay now we have this product models product image url okay so now let's get to the api parts now actually creating restful apis out of this model so we'll need a framework um let me go to my browser and see jungle let's framework this is the superpower that does um that makes um creating recipes a breeze for django developer so rescue api um go to installation installation you pip install jungle rest framework install jungle rest framework okay you can then add it inside install apps you understand okay and so on so let me get to it straight straight up now so let's do peep we're inside an environment so we use ppm instead of just ordinary people install jungle rest remote click enter it's going to start specification download to your folder great so now we have a jungle restaurant in our app next thing they tell you to do is to address framework to your installed app so rest framework just come here and come to your settings dot py under installed apps you could just add rest framework here press frameworks and you can just get going creating so now we have models the first thing you do after you have registered your press framework is to create serializers for each of your models so under your app create a new file you can call it serializers serializers of py realizes the ui okay so here you can just from from rest framework framework imports serializers that is the magic okay impulse realizes so this realizer is going to um what it's going to do is to is to serialize each of your models make them in form of adjacency realize to make a particular data object transforming to it json fireball just signified version of the same object so so what you should do here is to import also your your models so from dot models because we're in this particular directory this app directly imports category category book and product good so we're going to make specializer for each and every one of them so we make a category serializer and it stands instance um serializers dot model serializer realize the class so say class sorry class categorization analyzer extends this okay we have an inner class so excuse me so feeds takes feeds and takes model model okay so it's a it's a tuple so i'm gonna have a tuple id and then come to your category mode it's just title you just start to come to your model i think you'll just slide it just water a single field so tie two and moda is category remember we have imported it already okay let's do for the others too book serializer it's realizer class books realizer serializers dots realizer fix okay it's a toppo so we have a heidi id a primary key so you really don't need to specify that in your model so okay id and then um let's come here okay title category try to category okay um what other thing isbn pages price isbn pages price okay pages price stock description so description stock description image url description major area status and last but not least date created okay so field and the model mode is what is um book is already important so you should you don't need to worry yourself the next thing we should do is to create for products too so we have class products it's serializer products realizer realizes that models equalizer shhh id product and product okay product tag product type product name category price name category price price stock so meet you ariel image url status then date created okay and then the model is model is product which has been important so okay so now we have a serializer now for categories analyzer now for the three of them so next thing you should do now is to create your views so and create views we usually import from rest framework framework import generics so we're going to use um rest frameworks generic views and this um in this project so we can do a category list of categories so list category i have a generics dot um you have a list okay list create please create um let's just use a list api view list api view why do i forget class class list this category so okay so what you need here it takes two things it takes a query set takes a query set and then after query set it takes a serializer class it's analyzer class so that means we have to from here from dot serializers from this from this particular serializer class we created here so you're going to have to is import all of them each of them those realizers imports um categories your analyzer books realizer and then product it's realizing okay so query set is going to be we're talking about category that'll be category dot objects this is uh using jungles inbuilt orm object relational mapping tool so that's why we're doing this so on this realizer class will be category serializer you are done it's amazing very easy so very easy to create um api so we can create a um detail category like view a single category so generic dot retrieve have a retrieve update destroy api view all these if you read the documentation you see all of them but let me let me see let me see all the api views um i'm sorry class based views can you see so let's let me show you some of them so detail is a api view and one thing is ops awesome about this is um it's so easy the way you can customize things and make things easy for you to to understand you can see here this is the retrieve update destroy piv we have the list create api view so now let's let's go let's go on list create okay there's a list api view also so you can use the list create what this means is that you can use it to view all of them you guys need to um create that is add a particular resource so let's go on so for detail category that will be query set also you need query set is same thing as category.objects.org and then serializer class serializer class is is category serializer okay um let's create for book also plus list book that is a list of all of the books in our database so we have a list create api view we have a query set reset will be book dot objects dot all serializer class realizer classes um book serializer which has been already imported class details this one is to view a particular particular um book so retrieve let me just copy this retrieve update destroy apiv great so so we have a serializer class serializer class is oops realizer i've not even don't quit where is that is where is that is vocal objects that's all i have not imported the modes can you see he's giving me hero so also you should import all your models from those models as these models in this app import category then you see all the errors disappear now product so that's why i keep keep getting this this red uh vectors so let's do the same thing for product so list product as a list of all of them generate list create api view and then i'm going to have a query set is what product dot objects dot then i'm going to have a serializer class realizer class is um product serializer wait names due for a single product detail product generics dot retrieve update destroy api view let's copy this see i see it is it's very easy so query set is product and objects or less realizer class this product serializer now we've made our serializer now let's see we should do is to now write our urls so let's do let's create um that's great for category category list so it's to list all categories so have categories this is a list of all the categories firstly you have to import your views this is jungle so from dot views um imports import um all of them import this category this category detail category so i'm going to import all of them now um django api import this category detail category okay list book detail book okay list product detail product great so now we have our views imported into the url so so for this is list category no is this category dot as view okay you can give me the name say and say name this category categories or all categories whatever and you think it's okay so parts for one category is um a single category that's categories okay your primary key so that'll be i could i could just put a dash on the back for slash so that would be detailed category take category about as view we're using generic views here so that as you should be familiar so single category whatever name you want to give it so okay so list all books so you can say books okay so books are the list book dot as view name books this is the name of the of the url so books a single book i will key there's a detail group as view as view names single book parts of products products this product has view name can be products okay for a single product so we have our products so we have um product but as view name can be single product okay now i think everything is about though now uh views why are we getting errors i think my jungle extension is not i guess it's not um activated so now i'm going to use the the django inbuilt admin panel to add some some some of our products some some records so firstly the first thing now i've registered my house in the admin.py so the next thing i should do is to run migration rather in case some things have changed make migrations okay what's the issue retrieve updates destroy api view let's now have a retrieve update destroy api view retrieve update destroy oh i didn't spell it correctly can you see how did this pair retrieve correctly so okay that's the issue um retrieve and that's on that serializes um one of you sorry views so retrieve oh miss nan he here oh i'm sorry so sorry guys okay key let's go again we should clear out to clear all of this quarter now i turn make migrations great no changes detected atomizer py migrates great okay now we're good so i want to use the jungle at me to have some products so let's i will do that by creating a super user python manager py create super user i didn't spell that correctly also what is wrong with this create super user okay username oppesto okay email address it's luciano60 gmail.com password okay great super user created successfully so i can run my app so run your jungle project right on my.py run server and it's going to serve it's going to serve your project so so i can add those products using the admin panel so you see it's have that um http port 8000 so this is one two seven eight thousand so you get a jungle welcome successful screen so let me go to administrators okay so if you're logging faster okay can you see all of them uh category is not spelled correctly so i can change this and change this so to make it spell correctly category so you do this um you can do this in your inside your models for categories you can do it inside the models so you just have to need a nina class here that gives a babose name plural compose name plural of um we understand it's very correct enough okay um now let's see can you see it's now categories great we fix that so categories let's add different categories you can add so we need your style so um five stars lifestyle save and add another one um electronics electronics are mobile by force overflows okay um let's say home utensils home utensils uh for groups for books also we're going to need some things like um software development like books that our software development thriller thriller um what are the ones um adventure adventure um suspense what genre of books are there um all right general suspense adventure [Music] i don't know can't remind you now so let's say groceries or products groceries groceries um let's say let's say all my appliances all my appliances so how many categories do we have now okay we have one two three four five six seven eight nine okay we have some now um let's let's add some books let's have some books um can you see kangaroo is so awesome i love jungle so let's have some bruce let's say a reporter and they and the witch i don't know is this category can you see all your categories are loaded so um let's say adventure isbn let's say let's say let me just grab some random number and just put isbn or it must be 13 or one two three four five six seven eight ten level two thirteen exactly they think good how many pages does the book have 101 page one pages price is 24 or whatever or rand or whatever stock we have 20 of that book in the store um you just get some random i reporter description description great so let's copy that and put in a description so image url i think i have some viewings yes so reporter don't have any [Music] um okay you just put the immediate any of this status yes is available so save another notable let's say uh talking say talking category is suspense these are the same isbn pages are many of them i'm in pages so let's say it's a 90 page book prize it's just trap dollars stock we have just i'm just telling of that and stuff we just grab some random text and put yeah okay and then you can put this image also that's okay you can add some product also product tag sap p five zero zero one whatever you wanna put it um okay let me have some products on amazon looks amazon prime products let me let me see this prime product i just had something headphones it for this is this won't do anything fancy say headphone sony realtek headphone c cxx i don't know that's just electronics and mobile phones price is 50 stock we have just seven of them in stock and image let me grab some products here image here save another another product prototype [Music] p5002 name um um infinix finish or six mobile phones price is um 170 or whatever on or 15 000 nano stock of five of them and yeah these also want to add another p zero zero blah blah blah and then we'll have um let me check our categories you know whatever all that all my chances okay uh blender blender on utensils where it's just 19 dollars stock we have 100 of them the home appliances so i think that's okay so let's see all our products you have three products uh product object wow i think i didn't define a string for products products objects oh yeah yeah this is my mistake huh it was so rushing sorry guys okay reload state load our learner now we should see our products crazy great change okay fixed so now we can even see we can even browse our api now let's look at the url is api slash v1 so from your base url so this is your base url so i can just come here and do one to seven of these and put this at the back and am i trying to get books let's see books great voila can you see so we have books so um see this is the first book and this is the second book is a date created um category you can you can add more here if i want to add add another book like let's say let's say the start of ninja see our our book should have an auto so we should add also so i'm gonna do that so um category startup should be computers or whatever pages is 78 pages prices 25 stock is is 23 we have 23 so startup ninja start off just trying to see some real description report a startup is a this is just perfect this is just perfect so can you see this is why i like the jungle restaurant well it gives you this cool interface to browse your api browseable apis that's one of the killer of this of categorizer so you just click post uh remember we're using a under our um views so for group we we're using a list create so we can also create as well we have a post request also can you see now we have now we have what we just added now the startup ninja you can add another one whatever so you cannot list all the books click you see one two three books so same thing you want to browse for categories categories go to categories can you see these are the categories we have here lifestyle electronics and mobile groceries and so on so far so for products same thing products okay id one product tag re-tech headphone so on so good our job is about done so um it's really cool jungle is cool and rest removed it's just damn so um [Music] i think your api is done now uh the next thing we should do is to put our api on github we only created a can you say we only created a repository we you sort of put it on live repo so um this is html form you can have a raw data raw data from where you just simply just create your request based on json so this is how to create an api so i want to get the third product just create a slash and put three i get you the third one so and if i want to get the third book do i have three books there let's see great the third book c start of ninja what is the first book number one is harry potter and the witch and i will do this here so jungle is cool make sense you can edit it see see this put you can use it to change it we could just change the isbn number to zero zero zero zero like this and just just just watch it it's going to change so and then just put from exchange can you see zero see if just edit this you just update it let me talk in the restful times now i've just updated the api so thank you guys um in another video i'm going to create a user oh first before that i want to make the book i want to add a feed to the book the auto auto field so um [Music] can i just do that now or just add that later okay let me do that now so we we do that change inside our models now book isbn category let's put here an auto field let's put an auto feed let me stop execution although you don't need to stop execution jungle comes with auto reload by default you know there's a state loader watches for child five changes uh and uh renders when he runs the server so category auto auto is a model so max length max lens 100 as that's like auto name so okay now i've changed the model i'll need to rerun my migrations and then i also need to edit my serializer so auto is after category auto i think that's about it i'll change it in live here i'll change it here models serializers that's all that's all so come here and trying to run your migration again and people make migrations yes okay to island nominal level field so let me return it's because i've already uh i already have some few some um resources inside i'm trying to add a new feature what i should just do is to just code inside and just say no because so true or just put a default that i could change later default is let's say john doe see john doe so python is a py make migrations now migration should runs successfully great so python manager py migrates great good so item manage the py run server so we should now see the auto feed on one of the of the uh books can you see hotel john doe great so if you want to change this you can put who's the author of harry potter i think that's uh jd roll inside okay what's the spelling see is it like this oh he's a i remember eddie rollins i guess oh sure puts so put to update so you can see the autofill has been updated so um so right now to wrap up we just put our api on repo dude like real developer cell so these are apple what you should do is this same folder let me see okay do i want okay i want to cd and go to the back so and then let all my pip file p file up to me and the repository we're getting initialize and then we'll we'll handle our changes git add you can do like this or you can do a dot or gita all the files oh my changes okay and then hit commit m um first viewed was viewed for youtube okay great so origins have been now we need to add the remote origin so just copy the url copy then do a git remote add origin paste it here okay okay great so now we can now do a git push origin master wait now our code is on the remote repo just refresh this page and you should see your code if you see your project folder on github great so now we have my e-commerce api you can add readme okay ecommerce api is our hub these are models okay so um that's it for now guys thank you very much for taking some time i think i've spent more than an hour now thank you so much uh in the next um series we're gonna build a user um google integrated user model so that user can create all of these books and products and i also work on the authentication json authentication jwt using jwt and also work on and also work on adding course headers so cross origin resource sharing common origin resource change so thank you very much and please let me know in the comments below if this tutorial is a beginner friendly ass you like it so so i can improve on the next one thank you very much
Info
Channel: Faith Olusegun
Views: 24,650
Rating: undefined out of 5
Keywords: django, Django Rest Framework, Python, Web Developement, ecommerce, ecommerce api
Id: 5HpF5BZvDJ0
Channel Id: undefined
Length: 68min 5sec (4085 seconds)
Published: Sat Oct 31 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.