Creating a CRUD API with Django-Ninja #1 - Project and Database Setup

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there and welcome to another bug bites tutorial in this tutorial we're going to look at django ninja which is an api framework for django and as you can see on the documentation here it's a django rest framework but it's not the actual django rest framework this is an alternative to that very popular api library and we're going to dive into what django ninja can offer and what it is inspired by in this particular tutorial series so as it says here it's a framework for building apis with django and it uses python 3.6 plus type hinting and we're going to see what that means later on it also offers async io support and it allows you to define apis with automatic documentation so we're going to dive into creating an api for music tracks in this tutorial so let's get started with that now the first thing i'm going to do is pull up vs code and in this directory we have a requirements.txt file with two dependencies obviously django and of course django ninja as well so the first step here is to install and we can pip install dash r requirements.txt now i've already got these installed in my environment and it's probably best practice to do this in a virtual environment but that's up to you um so go ahead pip install that and once you've done that we can create a django project so django admin start app and sorry start project that should be i'm going to call this dj ninja and that should create a directory here called dj ninja with the django manage.pi script as well as a settings folder here with a settings and urls and whatnot now let's on the command line here we'll cd into dj ninja and what we're going to do is we're going to run python manage dot pi start app and we'll call the app tracks because what we're going to do is a music api for music tracks it's going to be a crud api um but in this particular video we're just going to do this setup we'll move on in the next video to some get endpoints and show you things like query parameters and path parameters and then in the final video we can move on to the post put and delete functionality so now that we have um an app called tracks that's given us a folder here and what we need to do now is add this to our settings installed apps so if we open up the settings.pi file go down to installed apps here and we can add tracks to that and that will add this new app that we've created here to install the apps now what i want to do is now show you the basics of django ninja we're going to create a test route that just returns a dictionary and what we're going to do is create a file called api.py and in that file we can import from ninja which is django ninja we can import the ninja app ninja api sorry um this is the object that is the top level object in django ninja and then what we can do is create an api object and instantiate that ninja api object and with this api object here we can create normal python functions that have decorated with api.get for example and we define as a first parameter the root that we want so this is us now using django ninjas this is kind of similar to flask but it's very similar to fast api and in fact django ninja is directly inspired by fast api and it uses pydantic and other tools to make it very similar to fast api so let's see that in action here we'll create a test route here called test and we just define a normal function and we can take in the request this is a standard practice and jungle the first parameter to views or api functions is the request object and for this one it's just a test route so i will return a simple dictionary with one key and one value and we'll see if we can get that working now now there is there is one more step here to get this hooked up what i'm going to do is go to the url stop pi we'll delete this big comment here and let's import from the tracks.api will import the and the api object so what we're doing here is in the tracks app we have an api.pi file so within that we have an api object so that's what we're importing here and the urls.pi from tracks.api import api and the reason we do that is because we can define now another set of url parameters prefixed by api and this django ninja api object that we've created has a urls attribute which will instantiate our urls based on these decorators so with that done we now can test our app and see if it's working so let's start the django management.pi run server command this is the django development server which should start up a server obviously we've not applied any of the default migrations yet but we'll do that in a later video and now we can go to our localhost endpoint i'll just copy paste that in here and you can see that we get back the json response and this is actually automatically serialized to json from our api root this is something that django ninja provides out of the box we return a simple dictionary and it's serialized to json now we're going to see in greater detail how we can serialize models and serialize objects and in the future videos but for now i think this is the setup complete for this part of the the app what we now want to do is we want to um create a database with the music tracks now what i'm going to do is get the tracks into this application so within the dj ninja folder i'm going to create a data folder and what we're going to do is grab i'm going to create a file called tracks.json this is going to be the raw data that we're going to import into our database and we can get that from this github link here which i will leave in the description and you can see this in more detail in the blog post i'm just going to copy the raw content into this json file and then what we'll do is define a djangle management command in order to import this data to do a management command we will create a management folder and we'll also create a commands folder within that and inside commands we can create an init dot pi and we'll create another command this is going to be the import command that's going to be called ingest tracks dot pi so let's paste this content in here and i will explain this code in a minute but we have one more step to do on top of that and let's create a model to represent a track in the database now within the tracks app we have a model stop pi file and what we're going to do is create a model here and i'm going to model this data off of the tracks.json file so if you look at this file you see that for an individual track we have five fields we have an id we have a title which is the name of the song we have the artist who created the song and we have a duration which is how long was the song in seconds we also have a last play now you can imagine this data is coming from an aptly spotify and this particular field last play is let's say the last time the song was played by anybody or by an individual user and that's another field here so we can create a model and i'm going to copy paste this for um to make this a bit quicker here so in the models.pi file i've already got that import we create a model called track and it has four fields here i said there's five that's because the id field here is automatically created by django when you run the migrations so we also have a title and an artist which are both models.car fields we have duration which is actually a float field because for some reason the data has um for example 200.5 seconds so it's a float and we also have lastplay which is a date time field so with that i'm going to stop the server and we can run pythonmanage.pi make migrations and that will create the model the m and the database i'll create the database table with those columns and then finally we can migrate and that will actually go ahead and create all of the the data within a sql lite database file so that is for the simplicity we're just going to use sql lite in this tutorial but this will work with any supported database and with that we can now explain about this management command now the only purpose of this is just to get the tracks.json data into a database most apis work with databases not fails so slightly more realistic example so within this file all we're doing is setting a path to the data file loading and as json data and then for each track in the data we are taking the last play field here and we're making a date time uh time zone aware date time object and then finally we take the key value pairs from each track and we create a track model with the keyword arguments as you can see here for each track in the data and finally but we use the bulk create command to create all of the tracks from that list comprehension here so that should create the tracks in the database we can test this out using manage.pi again and this one is a custom command and it's called ngs tracks and that's what we've created here so that should allow us to if we look at the sql lite file now i've got a vs code plugin for sql lite installed here so i can open the database and that should open an explorer down here and if we look at the tracks table we should be able to show the table and then you can see that it has actually and imported these tracks here um not the best view of this database but you can see it here got last play as a date time field duration and all the other fields here as well so that's the sequel light browser for vs code you can get that if you want so now that the data has been created we're now ready to create real api endpoints in the api.pi file so we're going to do that in the next video this is the setup video we've got to the end of that we've got a database full of music tracks now we want to create api endpoints to get all of the tracks and to get an individual track by its id as well as look at how to filter these tracks by query parameters an example of that would be if you look at the tracks.json you might want to filter all artists that begin with a particular letter all artists that begin with s for example we're going to use query parameters to do that in the next video thank you for watching and if you enjoyed this video please like and subscribe and we'll hope to see you in the next video
Info
Channel: BugBytes
Views: 990
Rating: undefined out of 5
Keywords:
Id: nC2kQylku0w
Channel Id: undefined
Length: 11min 17sec (677 seconds)
Published: Wed Oct 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.