Django: Getting Started Transcripts
Chapter: 3rd Party Libraries for Django
Lecture: REST APIs with Django REST Framework
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The Django rest framework is well, a framework for building rest interfaces. If that's new to you rest is a loosely defined protocol
0:09
for communicating with a server using http actions. The URL is used to uniquely identify a piece of data or a
0:17
query while the http method indicates what you would want to do with it. For example, a get with the URL for a person's ID will fetch
0:26
data about that person. A post for the same well would allow you to overwrite their data with new information.
0:33
As the data in question is almost always something with your models, the DRF gives you a way to wrap your existing models,
0:40
declaring a restful interface with very little code. The body of a rest request, also known as the payload can be in many different formats.
0:49
Out of the box the DRF supports, JSON, YAML, XML, variations on JSON, XLSX, LaTex in case you're an academic and
1:00
several of the export methods that pandas supports, like CSV, another kind of Excel, and even PNG files.
1:09
On top of all this, the DRF also has a browsing interface for your API. While you're debugging you can use the browser to look at what payloads are
1:16
coming back and manipulate your data. More information on the DRF can be found at django-rest-framework.org
1:23
So what exactly does the DRF provide for you? Well, it handles serialization of your models to and from a payload and it deals
1:32
with the different kinds of http methods used in rest. It gives you fine grain control over what's in your API by providing a series of classes
1:41
you can use to declare a view in the API which are kind of like the analog of the model form.
1:47
You specify the model you're wrapping and the class takes care of each of the http methods.
1:53
You can override any of the methods if you need to do something specialized in your interface. Of course as you're exposing data,
2:00
you probably want controls on it. So the DRF has a robust authentication and authorization mechanism.
2:07
You can also define the relationships between data models, a rest query might ask for books and return a payload that include info on the author
2:16
embedded inside the result. Because all of this is done over URLs. The DRF gives you the ability to control what URLs are used for what data and
2:25
where all this goodness gets mounted. Let's go play with some code to see what it's like.