Hello everyone and welcome to this new lesson.
Now that you've learned how to use the generic EPA view and mixing classes we can finally talk about
the generic classes that you're going to be using.
Most of the time because of their practicality and power in generous frameworks docs are called concrete
few classes because these are in fact the real concrete genetic views that implement most of the functionalities
that a developer might need.
How do they work.
Each one of their concrete view classes extends their generic EPA view class and those mixes that offer
the functionality that the class is meant to provide.
The class retrieve update EPA view for example will extend the generic EPA view class plus both retrieve
more than scene and update more Maxine because of their obstruction level.
They are surely the fastest to right and easiest to read but at a cost of being also the most magical.
It is therefore very important to keep in mind what we have seen so far in order to use them comfortably
knowing exactly if when and how to customize them.
Let's get started.
If we are invisible to your code and considering that in this lesson we're going to see how powerful
that concrete view classes really are.
I'm not going to comment this class we're actually going to write it.
Once again this time using a concrete view class.
So I'm going to copy the name class e-book least create EPA view which is going to extend generics dot
least create API view.
We can now simply set up the two attributes query set and see that I set a class.
This way.
We've only 3 lines of code.
We've created a perfectly functioning class that is going to do the same as the one that we've written
the previous lesson.
See if you go and have a look at the definition of list create API view.
We can see that it is basically the same class that we've written ourselves.
Now that doesn't mean that the view we've written is useless.
On the contrary it means that using the concrete view classes we're working at are yet abstraction level.
Let's not define that detail view.
So classic e-book detail API view which is going to extend generics dot retrieve update destroy API
view.
We can pass the same values for the two fundamental let's say attributes.
Then if you go and have a look at retrieve update destroy VBA view you can see that it's a class that
is using generic EPA view plus three different mixes so time to actually create some endpoints for our
views.
So let's go to that.
You are by file and I'm going to import the class and I'm going to create an end point so X1 ebooks
in the area can pass integer primary key.
The view is going to be e-book detail API view and any e-book detail.
You might remember from the previous lesson that integer decay is the default lookup field.
If you're gonna have a look remember that generic API view you see that you're in lookup field.
We got P.K. as a default.
So if we needed let's say to use as lag we might want to change lookup field and look up your role.
Q What are the argument accordingly.
So let's now run a development server and let's test our newly created in points us open up chrome India.
We can go to the Lesh API and here we see her 20 points.
So as Lesh e-books and data we get at least we've three books plus the HDMI all four that remember is
provided because we are actually using a class that extends generic API view.
Them check the details of a single book like for example 0 to 1 and you see we get the HDMI 4 and the
G sounded are already pre compiled and we also get a button to delete things this as a matter of fact
we can see all the h to be verbs that are allowed in D and point.
So let's say for example we needed to update the description for this specific instance.
We can use the practical HDMI all way and maybe you can just add something like updated pewter and you
see we made it put the request towards the endpoint.
We get 200.
Okay and the updated Jason of the instance.
Let's now go back to his words to the code and let us know write the review create API view class.
So first of all we're going to need to import our review analyzer and our review model.
Then right here below we can go ahead and create a review class review create API view which is going
to extend the generics dot create API view.
And here we can define query set as review dot object dot all and see realize the class as review serialize
it.
We have previously mentioned dead the concrete view classes are in fact among the fastest to right and
easiest to read.
But because of the abstraction level they're also among the most magical which means that most of the
software machinery that actually generates the behavior will be used.
It's a from us and therefore we need to know how to personalize them to fit our needs.
And in this case we want to write a class to create new review objects that we also want to automatically
link to a specific book.
Also because if we take a look at the review model we see that the relationship is expressed therefore
clearly we actually need to connect any book instance so we can do so by overriding deeper form create
method so def perform create which is going to accept Self and IT serialize her.
So what do we actually want to do with this method.
Well first of all we need to decide are we actually going to know which book we are going to review.
And that's pretty easy.
We can define a keyword argument in the real end point and then use that keyword argument to perform
the object look up.
So because of that.
And to keep the code in order I can actually delete this one.
We have to make an import.
So from risk framework dot generics import get object or for or 4 and we can now actually write the
code for our pure form create method.
So e-book became equals self doubt keyword arguments that get we want to get e-book P.K. which is going
to be the name of the keyword argument that we're going to define in the URL path based on this primary
key.
We can actually get these bookings as we want to review so e-book equals get objects for a form of e-book
with primary key equals e-book primary key.
And now we can actually call serialize or save into the save method.
We can pass e-book equals e-book so let's have a look at the code of create API view.
We see that clearly it everything from create model magazine.
So let's have a look at the code of that specific magazine from rest framework import axes.
Let's have a look and you see this is the create method which at the point calls self taught per perform
create.
Basically this method which just calls the safe method of the serialize her.
So in passing the e-book instance right here we are basically linking this specific instance of e-book.
We've got a newly created review object.
We now need to alter the serialized or the review sterilizer so that we can exclude from the sterilization
and the sterilization process.
The e-book so exclude equals a book.
Now let's go ahead and before we test the newly created class let's create another one.
Then what we're going to use for the detail and point so class review detail API view which is going
to extend generics dot retrieve update destroy EPA view and we can define the same attributes we've
defined up there.
So perfect.
Let's now define the new endpoints.
So first of all we need to import diffused light.
So one and
we can create the two new endpoints.
So first of all going to copy these and find like so we're just going to call review create VBA view
dot as view and its name is going to be a book review and the path is going to be ebooks.
Integer e-book.
Okay.
And then review we can change the indentation a little bit.
One two.
And then the second point and ear.
And we can now define the last end point.
So thanks so this is going to call review detail EPA view and dot as view is going to be called Review
detail and the path is going to be reviews slash integer primary key so time to test our new endpoints.
Let's run the development server that's now go to Chrome it is our e-book least up so let's try to review
0 to 1.
I'm going to API ebooks one review for words last year.
We're making a get request so we get a GDP for 0 5.
Method not allowed but nevertheless because we are using the developer browser API we get these HCM
CML form that we can use to actually create a new review.
Considering that we're excluding the book field we only get to review our total review and rating.
So I'm going to create this review preview author Michela review.
Great book.
Rate in 5 post and edit is we've just successfully reviewed the book and if we now go to the e-book
least and point we get HP 200 everything OK.
And you see now that 0 2 1 also shows a list with the reviews available.
Let's say we now wanted to update this specific review.
We might just go to API reviews 1 for what is Lesh and you'll see we get the details of this specific
review and both the h the email 4 and D J soon are actually pre defined and we can now just change them.
So let's say I want to update the review update.
Even better than I thought.
So let's click on puter and edit is just updated our review.
Okay.
So that was it for this lecture dedicated to the concrete view classes in Django this framework as you
can see they are really really powerful and pretty easy to implement even though we must take into consideration
that sometime we might want to check the source code of the classes to actually unveil a bit of their
magic allowing you to customize them when needed.
See you in the next lecture.