In this lesson we're going to create this utilize classes for that question and answer with models.
So the first thing that we need to do is to create an API folder and inside of this four that we can
now create the Syrian eyes.
It's not by fight let's import everything that we need.
So from rest framework the import utilizes and from questions that models import passwords and question.
First of all let's create as it serialize that.
So a class answer.
It's even nicer synchronize its dot model.
You realize that.
And even though the more they'll realize that a class provides us the standardized that fields based
on the model for out of a specific project we still need to add a couple of fields.
So first of all we want the string representation of the offer field.
Therefore let's define author as that ISIS dot string related field.
We've read only equals true and we're not going to define a couple of serialize that metal fields.
First of all a method field for created.
So that we can customize the order of day month and year in our data fields.
So she realizes dot serialize the method field we've read only equals through we're going to ignore
the updated add field that anyways is going to be available if you need to.
You can just customize the sterilizer and instead we want to define a likes count field which as the
name clearly suggests will give us the number of likes for a specific answer and then we're going to
ever use that as voted a field which is going to tell us if the request user has already left the like
on the answer where we're looking at.
And this of course is going to be extremely helpful when creating a user interface for our application.
Let's know defining class met up with model equals elsewhere of course.
Antonia we want to exclude three fields the question voters and updated at fields.
So here we can simply use the exclude attributes to define a list that we've question motives and updated
it.
Let's not define all the methods we need.
So first of all def get creative debt to self and investors.
And yet we can return things that's not created to dot s t r f time and yet we can't customize the way
we want our dates to look like.
I'm just going to use the American International way let's say to display dates.
So first of all the month then today is the digit.
And then the year.
So the date is going to be shown as.
Name of the month the date.
We have a digit and then dear.
So for example December 20 the 5th 2019.
Let's then define the other methods.
So get likes count
which is going to return instance that voters don't count.
And then you had a below we can define the last method.
So get user as voted an idea first of all we need to define request as self not context dot gets a request
and then you know we are going to return.
Is daughter.
Voters don't feel that many key equals request the user dot primary key.
And then here we can just return a dot exists and as we see later on we are going to have to pass the
request via context in a couple of views.
But for now we can just define the method.
Now let's define the question serialize it so class question either which extends serialize it's not
more that I see that of course and in the first two fields are going to be the same as the one we've
defined up here and then we can define as log as that ISIS dot the slug field and that's because we
want to pass read only equals true.
And then similarly to what we've done up here we want an answer with count.
So that's where US count equals C lies us dot c that i's that mattered.
FIELDER we've read only equals through and then also a user as password field which is going to be of
type C rise and method fields like the fields above we can now define class Meta model equals question
and we can exclude updated at.
And let us know define your methods so we can just copy it get created at
a b c d.
Like so then we're going to have def get answers count self and those and we're going to determine instance
that words that count were asked which is of course the related name we've defined here.
Let's go back to the C advisers.
Let's now define def get
user as answer self and use those and similarly to what we've done up here.
First of all we need the request and then we can return instance dot elsewhere and Dot filter this time
we've author equals request dot com user not exists.
And so similarly to date get use it is voted method get to use it as S word will return true if request
dot to use it as in fact password this specific question instance is you can see I've been passing the
read only parameter to the method fields even though they had already read only fields by themself I
got confused the error in the first one and then I've been using it for all the other ones but let's
remove and forget about them.
So now that I see realizes I have finally completed we are finally ready to start creating our first
views.