Hello everyone and welcome to this new lesson one of the key benefits of using the genetic views in
Django is in Django framework is that this is offered us a large amount of ready to use code that is
useful in most of the most common development scenarios CRUD operations in a model backed API for example
will be implemented in the same way in most cases and because of that reinventing the wheel every type
would be a waste of time.
Django this framework is a class called generic KPI a view that extends the EPA view class already seen
in previews lessons.
I think these are some very useful methods and attributes the generic API classes often use to weave
mixes which are classes that provide further functionalities to our views increasing their capabilities.
It is important to note and understand that as we will see the mixing classes provide action methods
such as leased or create rather than defining the handler methods such as GET or POST directly as we
did using the EPA view class before starting to write code.
Let's jump to the documentation first.
So here we are in January as frameworks documentation you can access the page related to the generic
API view class from the drop down menu API guide and then clicking on generic of use and then right
here on the left generic API view is we've briefly mentioned the generic API view class extends Django
test frameworks API view class adding commonly required behavior for standard at least in detail views
to control the behavior of the classes that we are going to write using the genetic API view class we
need to set some attributes which are going to be related to the rest of the code that we write in and
as you can see among these attributes we can find query set which is going to be used for returning
object from this view that we have serialize their class which clearly we are going to have to assign
to this that a class that we want to use for C rise in a day serialized in data and as we've already
said we often want to combine two day genetic EPA view class mixing classes which are going to be in
fact the classes that we provide the actions that are going tend to be used to provide the basic behavior
to our class.
For example you see we got list model Maxine that implements least in inquiry set then we got to create
model Maxine that implements creating and saving a new model instance and so on and so forth.
So it's definitely time to make some examples.
Let's jump to these to your code.
Okay.
If we are invisible to the code and the first thing that we need to do is to create a views that by
file within the API folder here we need to make two fundamental imports for this lesson from the rest
framework.
Import generics and from risk framework import mixes let's also import more data since they realize
there.
So from e-books that models import e-book and from ebooks that API the supervisors import e-book analyzer.
So let's write our view class e-book least create API view and first of all generics dot generic API
view.
We've also said that in order to actually provide some behavior functionalities to our classes we need
to mix the mixes with the generic API class in the context of these EPA view.
We can therefore use the least modal mixing and create model mixing.
So we need to actually use them before the generic API view class.
So Maxis does at least model Maxine and mixes not create model mixing before implementing the rest of
the code.
Let's ever look at a classic definition.
So this is the generic API view class which as you can see extends the EPA view class you see that here
we need to set these specific attributes or override some methods which we're going to do later on in
the next lessons.
But for now you see you can define for example query set and serialize her class which are basically
two of the most important ones.
And then you see there are some other very useful attributes for example lookup field which allows you
to specify a different field instead of the canonical primary key to perform object lookups and then
of course we can find implemented some other methods.
Let's now have a look at the code of the mixes.
For example the least modern Maxine go to definition and you see it provides the least method.
So in action method which will then provide the response based on that kind of action performed.
Okay cool let's see how everything fits together within our own class so here I'm going to define query
set which in this specific case is going to be a book that objects not all and then serialized class
equals ebooks utilize it.
We now need to define the handler methods in this case get and post which will then return the behavior
provided by the action methods.
So def get a screen to accept self request documents and keyword arguments and it's going to return
self-taught at least.
And yet going to pass the request.
And then how did you humans keyword arguments.
And in case you're new to this kind of definition arguments and Q What about arguments.
This is just a convention that we use to actually tell our functions that they might need to manage
a variable number of arguments and keyword arguments without needing to know exactly what the other
Mandarin for example is going to require.
Now we've got post def post which is going to accept the same parameters I can in fact copy everything
like so.
But this time I'm going to call self that create we now need to connect of you to where you are real
and points from when to create you have to start by five weaving the API folder in here from Django
that you rails import bath and from ebooks dot API dot views import e-book at least create API view
now your real partners it's going to be at least it's always path books which is going to call e-book
least create API view dot s view and as a name we can pass a book
as always we now need to connect the three year old stood by files so I'm going to import include.
I'm going to use it right below your SO path API include ebooks API noted that we can now run our development
server which we actually need to configure so at configuration.
Python.
I'm going to comment out as always no threading.
And now we can launch it
everything works fine.
Let's go to Chrome so let's go to slash API slash eBooks.
And clearly at the moment we get an empty list even though the status quo is to undo it.
OK.
And that's because we haven't added yet any instance.
But as you can see we know where the very useful form that we can use to actually define the Jason that
is going to be sent.
This is another very useful feature of the browser API when used in combination with a view that's a
little bit sophisticated.
Let's say like the one we created we have the generic API of your class so let's create an instance
for example zero to 1 author Peter Thiel description notes on start ups or how to build the future.
And he was published on this date 0 9 16th two thousand fourteen.
Let's post and it is a beautiful instance.
Perfect.
Let's add another one.
So I'm going back to the first point.
Maybe computer networks by Andrew
Tannenbaum description our full introduction to computer networks and to set the publication date.
We can also take advantage of a very powerful widget you see that is provided by the browser API.
Even though I actually don't remember the publication date of this book so I'm just going to write 0
1 0 1 2010 something like that post and if we now go back to the midpoint and we got a list of ebooks.
Okay.
So that was it for this lesson about genetic EPA view class and maxis and in the next lecture we're
going to start talking about their so-called concrete view classes.
Basically class based the EPA views that are based on top of the genetic EPA view class and several
mixes.