I recently published a new video about a job post by Cybercoders that requires REST API experience. In that tutorial, I point learners to my docker repository for a django rest api, scottyfullstack/basic-rest-api.In this post, I'd like to cover how I made that and point out some important features of the Django Rest Framework.
So what is a RESTful api?"REST is acronym for REpresentational State Transfer. It is architectural style for distributed hypermedia systems and was first presented by Roy Fielding in 2000 in his famous dissertation."In our use case and so many others, there is typically a front end application that is decoupled from the backend services (db, apis, etc...). A REST api presents json data that a front end app consumes and presents in user friendly way.To start us off easy, we are going to only be using GET, POST, and DELETE methods in this tutorial.To start, lets make a directory in our projects called rest-api.First, lets create a virtualenv for our development in our terminal and activate it (I use the fish shell so if you use bash don't include ".fish")Now let's pip install our project requirements:Once those are complete lets fire up a django project, cd into it and start a project called "product".And now your project should look like this:
Configuring the settings.pyGo ahead and open up the settings.py file found in the main project folder restapi. Most of these will stay as defaults.First, lets make sure we have the imports needed. OS should already be included by default, and we will be using a library for our postgres database called dj_database_url:Since we are going to be running this app on an EC2 instance and may decide to run DEBUG mode (not to be used for a real production instance), we will set ALLOWED_HOSTS to anything.We will need to add what we pip installed earlier as well as product to the INSTALLED_APPS settings.Next, make sure your CORS middleware is added and you allow all origins (You don't always want to do this in the real world).The database is going to be tricky depending on how you approach this in your practicing. In the video tutorial, I set up an RDS postgres instance that we would connect to in this file.
We will make a hybrid database setting to account for if we are running locally with sqlite or passing a db string.hence the 'DATABASE_URL' variable being verified.and then you want to open up your rest framework permissions.
!!! We are allowing GET, POST, and DELETE here. DO NOT DO THIS ON A REAL WEB APP. This is strictly for our quick api testing and proving we understand it to employers. We are disabling Authentication to POST and DELETE to our site. You've been warned !!!Finally, let's make sure we set the STATIC_ROOT somewhere in the file for our API styles that are included with Django Rest Framework.That's all for our settings.py file. Again, we are not using a hosted database here. We are using the sqlite db file to store our temp data. So when you delete the container the data will be wiped.
Product Setup: Serializers, Model, Views, and URLHead over to your product app folder (restapi/product/*)Open the models.py file and add your fields. In this case we are just going to have title, description, and price.Now make a new directory call api inside the product dir and create the following files. (restapi/product/api/)Let's start with the serializers.py file.According to the official documentation:"Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data."In ordinary terms, this serializer is going to take our model data and turn it into a json object for our frontend to consume.Not too much going on here. As you can see we plug our Product model right into the serializer format.Next, open the view.py file and add the following:Here we have two views. The first one, ProductListView, is a list of all our model objects. The second one, ProductDetailView, is a detailed list for a single object by 'id' (note the filter_fields). The detail view will be instrumental for our DELETE method.Finally, in this directory we will need to populate the urls.py.This configuration consists of two paths, one for our list view and another for the detail. the <pk> will be our object 'id'.And that's it for our product api.We have one more change to make to make this run. We have to update the urls.py in the main project folder ( ./restapi/restapi/urls.py).As you can see above, we added the 'api/' route that includes the urls.py file we create in '/product/api/'We can now run our app but will need to make and run migrations.Pull up http://localhost:8000/api and try it out. There is an html form for you to test with or you can always use Postman.
Extra CurricularIf you are interested in dockerizing this to play with here is an example requirements.txt, Dockerfile (with entrypoint file) and docker-compose.yml file that I use for this app.That's all i've got. Happy coding and remember, if you would like updates join the newsletter and slack channel of the home page for updates and if you have any questions!Until next time,Scotty
Disclosure: scottyfullstack.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com.