#100DaysOfWeb in Python Transcripts
Chapter: Days 69-72: Django REST Framework
Lecture: Create a simple Django REST API
0:00 Alright, so we did the Django command we imported quotes we have data in our database so we can start building our API. We also already installed
0:12 Django REST framework into our virtual environment. So, let's start by building a new app. And the way to do that in Django
0:20 as we saw in previous lessons is to use the startapp switch of the manage.py command. So in my root folder of the project I type startapp api.
0:39 That makes a new folder or app with the standard Django files. And next, we want to make a route into the main URLs file. So at the project level
0:56 in the main app, mysite and go to URLs. And I'm going to add a path here to api. And that's going to include the api.URLs file.
1:20 Next, we want to set REST framework in our settings.py. So, let's distinguish them by
1:38 Django native, external and own apps. And we've made api.
2:06 And then to make a basic API, we need three files. URLs.py for the routes, a serializers.py to transform the ORM data into JSON
2:24 and a views.py which is already there. So, let's start with the routes from Django. URLs input path from our views, import quote
2:41 API view, and we still have to write that. That's next. We set URL patterns. Just a list, path and at the root of the api subdirectory.
3:01 You will load in a quote, API view as_view. That's it. Next let's write the serializers.py file from rest_framework import serializers
3:25 from quotes.models import Quote We're going to inherit from serializers.ModelSerializer. We call our own class, QuoteSerializer.
3:54 Similar to forms, as we saw in previous lessons you can just define a couple of attributes in a meta class.
4:04 And we don't have to really know much about meta classes just syntax. We just have to define the quote model
4:11 and the fields we want to have in our API, which is quote, author, source, cover, and source and cover are optional fields
4:26 but we should still present them as possible want to add. And user. So that's it for serializers and then we going to use that in our view.
4:42 from rest_framework import generics Then we're going to load in the QuoteSerializer and we're going to need our model.
5:04 And we're just going to do the listing of the quotes to start simple. So we're going to make a class-based view
5:15 QuoteList inherited from generics.ListCreateAPIView And that's the nice thing that generics from the REST framework
5:29 has all these classes already defined and that makes us that are view can just be two lines of code which is the query set just all the objects
5:46 and the serializer class which is QuoteSerializer. So, generics gives us ListCreateAPIView which defines two attributes we need to set
5:59 on our inherited class which is queryset and serializer class, query set loads all the quotes from the model.
6:07 And the serializer takes care of transforming the data into compatible API format which is JSON. And that's it. So I got URLs. I got views
6:24 which work with the serializer we defined. And the same as I said about views. The serializer is also very little code
6:34 thanks to the abstraction in the framework. So we have serializers, a ModelSerializer. We just inherit from that. And it's all defined
6:46 and we re-use all kinds of nice behaviors defined in the base class. That's really elegant. Okay, let's see if this works.
6:55 Actually, this was my run server terminal, so let's go back. Okay, I got some syntax issues here in URLs.
7:17 And that's because I actually call this quote list and quote list. That works.
7:36 Let's, this is still the base page but if I go to my new endpoint voila, this looks nice. Look at the rich front end that Django REST ships with.
7:53 And here are all my quotes. I cannot do specific quotes yet because we have not defined a second class-based view to work with details
8:03 but that's what we're going to do in the next video.