Django: Getting Started Transcripts
Chapter: 3rd Party Libraries for Django
Lecture: Writing DRF code

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The RFIs just an app. A fancy powerful app but an app nonetheless. And of course it needs to be registered. So I'll open
0:24 Note that the name of the app is different than the name of the package
0:28 rest_framework is the name of the actual module because I want to use the browsing interface, I'm going to need to register the URLs
0:35 that are used for authentication. Let me open up alexandria and this is the line that registers the packages off URLs
0:54 I'm going to demo the DRF by writing an API for our author data. I need two things for this.
1:00 Something that serialize is the author objects and a view for the API. I'll start by creating the serializer inside of catalog
1:17 I need to import DRS serializer module along with the author object that I'm going to serialize
1:31 and then reminiscent of model forms, I create a serializer class based on the author model.
1:52 See what I mean? By being like model forms, you specify the model that this serializer is associated with along with the fields that
1:58 you want serialized and you're good to go. With the serializer in place, next up is a view for the API.
2:06 I'm going to put the view with the other views for catalog. Let me open up, there are two imports that I need.
2:24 The API view decorator indicates that this is going to be an API view and the response object is the DRF
2:31 equivalent of an http response object that you would use in a view. And this is the serializer I just finished building, now to add the view.
2:47 Let me scroll down to the bottom here.
3:10 Structurally, this is similar to the views you've written before with just a few small differences.
3:15 First the decorator indicates that this is an API view and the get parameter says it's only going to support the http get method.
3:25 If you were going to do a full interface, there are class based views that take less code than writing this for each of the http methods.
3:33 But this is a quick way if all you're providing is a read only interface. On line 63, I query all the authors and then I create a serializer
3:43 object based on the one that I declared before it takes the query set result of
3:48 authors and an argument that indicates that there are multiple things being serialized here. I then get the output by using the serializer objects,
3:57 data attribute and wrap it in a context dictionary. All of this gets put inside a response object and the view is ready to go
4:04 The last thing to do is register this URL. Let me open up catalog, if you're going to do a lot with the DRF
4:21 it has a full route management mechanism that helps you manage many different endpoints with very little code.
4:27 But for this quick demo, I'm just registering this view like any other. Let's go take a look at this in the browser.

Talk Python's Mastodon Michael Kennedy's Mastodon