In this lesson we're going to be the two views related to the answer model.
The first view will allow us to create new and scary instances which means that in fact will allow us
to answer specific questions while the second view will allow us to retrieve all the questions instances
for each single question.
First of all there are a couple of functions and classes that we need to import generics.
Then from the test framework dot generics import get object or form for from the rest.
Framework that exceptions import validation error.
And then of course we need to import the deep elsewhere more then and also the answer is a plus.
Now below we can define a class password create API view which is going to extend generics dot the API
view.
We've quietly set equals answer dot objects dot all asset class equals and sort of set of eyes and ears.
If we want to be explicit on the views level as well we can just copy the permission classes attribute
using is authenticated.
Of course this new class is going to be really powerful for a couple of reasons.
First of all it will allow us to answer a single specific question based on as like parameter past VII
and bind it will allow us to bind the request dot user as an author for the newly created is this.
And last but not least it will check if request that the user is already asked where the question.
And in that case it will raise a validation.
All of this is going to happen inside the perform create method.
So yeah I can modify the code like so.
First of all request user equals self that request the user then we need to get the keyword argument
flagship equals self not keyword arguments and not get it like it.
Let's now get the question instance.
To answer.
So get object or photo for the answer to it more than we've slug equals keyword arguments like to deceive
me.
I don't we can no pass question equals question but before calling this save mattered we can check if
they use that is already asked where these specific question is this If the question dot answers dot
feel that we have also equals request user.
So if this exists we can then raise a validation error with the message you have already.
That's where this question now that the class is complete we can create an appropriate endpoint.
So your path questions slash slag up of type lag and then answer and we want to call question view dot
answer.
Create a gave you dot as view.
Let's also add a bit of indentation XOR and then name equals create that's.
It's now time to test the new endpoint.
So let's run the development server and let's move to chrome.
We are of course redirected to the logging page but we need to go straight to the browser API so slash
API authentication credentials were not provided.
Let's look in as admin we have questions and point let's grab these questions lag so coffee and paste
and then what is for Slash.
And yeah we see from the title answer create API.
Of course we get method not allowed because this first one of course was a get request.
So let's create a new answer.
Let's write something like hello world post we get a small error cannot retrieve keywords log into field
and that's because we are we fast the asked Would model instead of the question model to get object
or form because of course we are getting a question instance and we also need to change questions to
question so that we can filter the query set of answers for this specific question needs this.
Let's restart the development server and let's go back to Chrome and well let's get the details of the
same question we wanted to answer first.
So and So you see that of course clearly now ask where count is still 2 0 right.
So let's answer the question.
So now maybe we can add something like first and square instance post and eateries are first asked Twitter
and if we now go back and check the details of the question we've just answered.
We see that as what count is now one and now the receipt.
Yeah maybe we could also add a comma.
So let's go back.
The results of your code API Isaias so we we can add a comma like so and also here.
Let's restart the development server let's go back to Chrome and let's make the same get request well.
Much better.
Let's go right the second elsewhere related view so that we can get all the answers for a specific question
given its lag.
So let's go back to visible pseudocode India.
We can create class question elsewhere least API view which extends generics not least API view India.
We can just copy seen it as their class and permission classes just to be explicit and we don't need
to define the query set attribute because it's better to just overwrite to get where set method so self.
Yeah we can get the same kind of like attribute these used in point we love zone is like a keyword argument
of course we can return that's where dot objects dot feel that question slug equals Q What is that like.
Well maybe we can just call it answer at least EPA view it's more concise and then yeah we can also
I'd ordered it by negative created that let's not create the appropriate endpoint.
So yeah well we're going to have basically the same endpoint as you're going to just copy it so comma
this is going to be and Swiss it's going to call us at least EPA view it's just going to be called and
square at least let's restart the development server and let us go back to Chrome once again.
So now to get all the answers for this specific question we can go to the same endpoint and swaps for
what is in ash.
And of course we now get our first answer where each instance and if you try to answer the same question
more than one.
So again as you can see we get the validation error you have already answered this question.
Perfect.