Python Django + MySQL CRUD API Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's install python we will be installing python from the official website also choose to add python to path let's verify the installation from command prompt the installation was successful let's install visual studio code visual studio code as a popular lightweight code editor from microsoft it is mostly used for front-end web development let's install postman we will be using postman to test our rest apis let's install my sql workbench we will be using my sql workbench to connect to a mysql database and create the necessary database objects bye let's install the necessary modules needed for our django project first let's install the django module to create rest apis we need to install django rest framework by default the django project comes with a security that blocks requests coming from different domains to disable this let's install django cores headers module now let's create the django project open the command prompt in the desired folder and type the command django admin start project and the name of the project let's open visual studio code inside the project path let's take a look at some of the important files in the project init.py file is just an empty file that indicates that the given folder is a python project or a python module asgi.py is the entry point for the asgi compatible web servers wsgi.py is the entry point for the wsgi compatible web servers urls.py file contains all the url declarations needed for this project settings.py file contains all the settings or the configurations needed for the project manage.py is a command line utility that helps interact with the django project let's simply run the project and see how it looks in the browser just type python space manage py space run server the app is now running in the port 8000 let's copy the url and open in the browser what you see on the screen is the default template that comes with every django project now let's create an app in our django project quick difference between projects and apps the folder structure that you currently see on the screen is called the project a project may have multiple apps for example you can have one app which acts like a blog or maybe another app which acts like a survey form currently this project does not have any app let's create one app to implement our api methods to create an app we need to type this command python space manage.py space start app space the name of the app next let us register the app and the required modules in settings.py file in the installed app section let's add rest framework course header and the newly created app we need to add the course headers in middleware section as well we will also add instruction to enable all domains to access the apis this is not recommended in production instead just add only those domains that needs to be whitelisted let's create the models needed for our app we need two models one to store department details and another one to store employee details the department's model will have two fields one to store an auto-incremented department id and another one to store department name employee model will have five fields employee id employee name department date of joining and photo file name which stores the uploaded profile picture file name we will be using my sql as the database to create the tables from these models let's open up mysql workbench i have already procured a mysql server from azure database service and will be connecting to the same let's create a database for our app to connect to mysql from our python app we need to install the database adapter let's install it with the command pip install py mysql let us now add the database connection details in settings file with python3 there is an issue with this database client to fix it we need to add couple of lines now let's create the tables from these models let's write the command to make migrations file for our models after executing this we can see a migration file which tells us what changes to the database will be done once it looks fine we can execute the command to push these changes to the database let's now refresh the database we see that the database tables are now created let's run select query on them we see that currently we don't have any data let's insert some record into it bye let's create serializers for our models serializers basically help to convert the complex types or model instances into native python data types that can then be easily rendered into json or xml or other content types they also help in deserialization which is nothing but converting the past data back to complex types let's now start writing the api methods openview.py file we are importing csrf decorator to be able to allow other domains to access our api methods we need json parser to parse the incoming data into data model let's import the models that we created and also the corresponding serializer classes let's write our api methods for departments table the method will receive an optional id which we will need to use in the delete method in get method we will return all the records in json format we are making use of serializer class to convert it into json format the parameter safe equal to false is basically used to inform django that while we are trying to convert to json is actually a valid format and we are fine if there are still any issues in it let's write post method which will be used to insert new record into departments table we are parsing the request and using the serializer to convert it into model if the model is valid we save it into the database and return success message next let's complete the put method which will be used to update a given record first we are capturing the existing record using department id next we are mapping it with new values using serializer class and save it if the model is valid finally let's implement the delete method we will be passing the id to be deleted from the url let's now add roots to specify the urls for our api methods add a new file urls.p the delete method will receive id in the url next we need to include these urls in the main urls.py file now let's test our api methods let's first start the project using the command python space manage.py space run server bye let's test get method which returns all data from the table we see that get method works as expected let's test the post method to insert new data the post method also works and we can verify it by calling the get method again let's test put method and update a record the put method also works as expected finally let's test the delete method the delete method also works as expected let's now implement the api methods for employee table bye let's add the url routes as well let's now test these apis we see that the employee api methods also work as expected let's add an api method to save the uploaded profile picture to store the images let's first create a folder in our app to be able to use this folder we need to add some instructions in the settings.py file now let's add the api method we are making use of default storage module to save the file once it is saved we simply return the file name let's add a url route for our api method let's now test the api method by uploading a file we see that the upload file api works as expected you
Info
Channel: Art of Engineer
Views: 46,110
Rating: undefined out of 5
Keywords: python django, mysql, rest api, crud api
Id: WuOjWTnnrfw
Channel Id: undefined
Length: 24min 47sec (1487 seconds)
Published: Tue Jun 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.