WEBVTT
Kind: captions
Language: en

00:00:00.030 --> 00:00:05.279
Let's create a simple hello view set
class to demonstrate how you would use a

00:00:05.279 --> 00:00:11.219
view set in practice load up the code in
the atom editor and head over to the

00:00:11.219 --> 00:00:17.160
views dot py file where we created our
hello API view class in the previous

00:00:17.160 --> 00:00:22.020
section we're going to create a new
class below this called hello view set

00:00:22.020 --> 00:00:27.660
before we do that we're going to import
view sets from the rest framework so

00:00:27.660 --> 00:00:37.860
type from rest underscore framework
import view sets then below our hello

00:00:37.860 --> 00:00:46.770
API view let's create a new class called
class hello view set and we're going to

00:00:46.770 --> 00:00:54.840
base our class on view sets dot view set
which is the base view set class that

00:00:54.840 --> 00:00:57.700
Django rest framework provides

00:00:57.700 --> 00:01:00.410
We're going to add a doc string that says test

00:01:00.410 --> 00:01:09.150
API view set and then we're going to add
some functions to our view set so as I

00:01:09.150 --> 00:01:13.890
explained previously the functions that
you add to a view set are a little bit

00:01:13.890 --> 00:01:18.659
different from the functions you add to
an API view in an API view you add

00:01:18.659 --> 00:01:23.490
functions for the particular HTTP
methods that you want to support on your

00:01:23.490 --> 00:01:33.210
endpoint for example HTTP POST HTTP put
HTTP patch and HTTP delete for a view

00:01:33.210 --> 00:01:37.650
set you add functions that represent
actions that you would perform on a

00:01:37.650 --> 00:01:44.220
typical API so the action that we're
going to add is the list so a list is

00:01:44.220 --> 00:01:51.990
typically a HTTP GET to the root of the
endpoint linked to our view set so what

00:01:51.990 --> 00:01:58.130
this normally would do is list a set
of objects that the view set represents

00:01:58.130 --> 00:02:05.369
so let's create our list method by
typing def list and you need to give it

00:02:05.369 --> 00:02:11.020
the parameters self and the request object
that is passed in by Django when

00:02:11.020 --> 00:02:12.600
the request is made

00:02:12.600 --> 00:02:17.370
Then we'll add a doc
string that says return a hello message

00:02:17.370 --> 00:02:23.830
and then what we'll do is just like we
did for the get request in our API view

00:02:23.830 --> 00:02:29.890
where we created a list of features that
the API view has we're going to create

00:02:29.890 --> 00:02:37.540
the same list for the view set so we'll
type a view set equals create a list and

00:02:37.540 --> 00:02:44.100
we'll write the first item of the list
that says uses actions list create

00:02:44.100 --> 00:02:50.620
retrieve update partial updates

00:02:50.620 --> 00:02:52.270
and that's the first item of the list the

00:02:52.270 --> 00:03:01.520
second item will be automatically maps to URLs using routers

00:03:01.520 --> 00:03:11.710
and then the final item will be provides
more functionality with less code so

00:03:11.710 --> 00:03:17.110
these are three typical features of a
view set and now what we'll do is we'll

00:03:17.110 --> 00:03:24.270
return this object by doing return
response and we'll give it a message

00:03:24.270 --> 00:03:34.770
with hello and then we'll return a view
set and assign this new a view set list

00:03:34.770 --> 00:03:41.500
okay so when the request is made to our
view set to the root URL

00:03:41.500 --> 00:03:46.030
of the view set it will call this list
function which will create this list

00:03:46.030 --> 00:03:52.600
object here and then it will return it
in a response in our API so make sure

00:03:52.600 --> 00:03:56.470
you save that file and then we'll move
on to the next video where we will

00:03:56.470 --> 00:04:01.830
configure a URL to point to our view set.

