Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Sometimes you need to control exactly which HTTP methods your viewsets will respond to.
Welcome back.
0:00
Previously I created a new
version of the API and
0:02
used resframeworks, view sets and routers.
0:04
This allowed me to combine all of
my grade views into a single class.
0:07
And the router automatically
generated the URLs.
0:10
I also wrote a custom view method to list
coarser views on the course view set.
0:12
I still have one problem to solve
in this V two implantation though.
0:17
My review V sec class extends REST
frameworks model views that class.
0:21
Which hopefully provides views for
creating destroying.
0:25
Listing, retrieving and updating reviews.
0:28
But since I created that
review list view and
0:30
a view that lists all the reviews on
the site doesn't seem really useful.
0:32
I need to get picky about which
views this view set provides.
0:36
So to start customizing these views,
or the view sets rather,
0:41
I need to bring in Django REST
frameworks mix ins.
0:45
Mix ins are, if you're not,
if you're like, I know that word, but
0:50
I can't know what it is.
0:54
Mix ins are a little small classes.
0:55
That you mix in to create larger classes.
0:58
So it's a way of doing inheritance
instead of composition.
1:02
We'll talk more about
that in later courses.
1:06
So.
I wanna show you what the model of view
1:08
sets class definition looks like.
1:11
So we're using this thing here.
1:14
I wanna show you what
that actually looks like.
1:15
So, if I was a do model, view, set and
I was working for Jango REST framework.
1:17
This is what I would've put in.
1:23
So, mixins.CreateModel\ mixin,
1:24
mixins.RetrieveModelMixin,
mixinUpdatemodelMixin and
1:29
destroyModelMixin and, two more,
1:38
mixins.ListModelMixin.
1:44
And then, the viewsets.GenericViewSet.
1:48
And then, pass.
1:53
So.
1:57
Or I might have passed for now least.
1:58
So model view set just a combination
of generic view set and
2:00
then all of these mix ins,
your mix ins come before your
2:04
class that they're modifying so that
stuff gets evaluated in the right order.
2:07
So anyway this is what's normally there.
2:13
So the Create model mix in is what handles
the creation, the retrieve model mix
2:15
in is what handles the retrieving of a
single object of a single model instance.
2:20
The update is what handles
updating a single one,
2:26
destroys what handles destroying one and
2:29
the list is what handles getting
the query set for the full list of them.
2:31
So, you can see each one of these things
just does one little specific bit.
2:36
And then the generic view
set is what handles like,
2:40
I'm going to make all these views
that do this stuff go, right.
2:42
So if I want to do all of the stuff but
I don't wanna have the list,
2:47
it would make sense to just build
this again instead of using this.
2:53
right?
3:00
I got an easier way though,
you ready to see the easy way?
3:01
Here it comes.
3:04
Delete that, rename this to ReviewViewSet,
Delete those two lines.
3:06
Hey, look at that!
3:14
Now I have a model view
set that doesn't do lists.
3:16
Cuz that's what I want.
3:21
So, hey cool!
3:23
[LAUGH] We just did it right there, yeah.
3:25
Isn't that amazing?
3:28
So this should be able to work.
3:30
I should be able to go to v2/reviews and
method get is not allowed 405 right.
3:32
It's not exactly a 404.
3:41
It's not like hey that doesn't exist but
it's a 405.
3:42
The method isn't allowed,
I'm not allowed to use get on reviews.
3:45
I can't just get a whole
list of reviews and
3:49
APIs get the 405,
then your okay I can't do that.
3:52
But if I try to get just one review
because I know I have a single
3:56
review in there.
3:59
I get that actual review, right?
4:00
I can get that review, I can put,
patch, delete, header, options it.
4:02
So, awesome!
4:05
Effectively I've created a whole
second version of this API.
4:07
I wrote less code than I did before.
4:11
I think that's pretty impressive.
4:13
It's a good idea to look over
the documentation for both routers and
4:16
view sets, so I put links to
both of them in the usual place.
4:19
The teachers notes.
4:22
At this point you've learned the majority
of creating API views with Django REST
4:24
framework you started very low level with
API view moved up to generic views and
4:27
then finished it all off
with view sets and routers.
4:32
All of these views have
been class based views, but
4:35
it is possible to create API views
with traditional function based views
4:37
using some decorators that
REST framework provides.
4:41
If function based views
are more your style.
4:44
Check out the teachers notes for
a link to the relevant documentation.
4:46
You need to sign up for Treehouse in order to download course files.
Sign up