Skip to content

Django \ Django Rest Framework

Mikey Manoguerra edited this page Oct 10, 2019 · 1 revision

How does the Server Work?

The server logic is written in python The database is a PostgresQL instance hosted in a docker container. The server uses two major frameworks/libraries: Django & Django Rest Framework.

What is Django?

The django project brings a lot of out of the box utility to the project. It has a file structure designed to get projects up and running quickly. It is closely tied to SQL databases, and thus has robust class hierarchies: for model building that is translated to SQL behind the scenes, for easier Migrations , and for making all types of queries. It also comes with a bunch of security and authorization built in.

What is Django Rest Framework ?

While Django on its own is set up for MVC programming (model view controller), Including Django REST Framework (DRT) turns the server in to a REST API. DRT has a structure of 'views' and 'serializers'.

The view accepts the incoming request, and uses the tandem serializer to make queries on the database, and 'serialize' or transform the data into a structure that can be sent to the front end. The view/serializer structure is usually closely tied to a specific database model.

For the a front desk event, for example, a request hits GET /api/front-desk-events/, The view gets all the FrontDeskEvent objects, passes them to the serializer to create a list of objects, and then the view returns the the list as JSON to the front end.

This project utilizes the high level viewset.ModelViewSet class, which obscures most of the logic out of view. This viewset, out of the box, handles the common http verbs. As things get more complicated, the request methods can be redefined and related models joined in.

How Does it all work?

A request comes through and matches with a specific endpoint path, as registered in urls.py. This is how django captures incoming requests, and matches them with the correct view. Next, the request enters the view and the user is authenticated in viewsets.py.

If the user belongs to the correct group related to their request, The view handles the request by querying the database, filtering as necessary, and passing the queryset to the serializer to be properly transformed from queryset class to data that can be turned into JSON. Lastly, the view returns the response, where the front end can accept the response and display it

Want to learn More?

A great place to keep learning are both the Django and Django Rest Framework tutorials. They each take a couple of hours, but are both highly regarded and are comprehensive introductions to the robust tools that they are. I have barely scratched the surface here in my description, and I find new useful methods in the documentation all the time!