Okay, let's finish our function here.
So basically for creating new rating, we'll need to have three things.
So ratings needs to have a movie user and stars.
So coming back to our views, we have our movie, we have our stars.
So we can actually put it in a variable here.
So stars will be equal to request.
Data and then we can do stars like that because the stars are are in the request data.
So we know so we can assign this to to the variable.
And then what's missing here is the user.
So I can do user and normally the user is part of our request.
So request I can show you the user.
So if I will do print.
User here.
I will save it now and then refresh it.
So let's try to print.
I we'll use the same method again.
And coming back here, you can see user is anonymous.
Why this is happening?
Because at the moment Django can't really tell what user am I?
Because I'm not really logged in.
We haven't done that part yet and we don't have authentication yet.
Normally, if you will pass a token, as we discussed that in the previous section, if you have the
token authentication and with that request that that method, you will send an authorization token,
then request user will have a information about that user.
But at this point we don't have it yet, so we might save that for later.
But for the moment I will just put a comment here and we'll come back to this.
What we can do is we can create our static user.
We know that we have only one user in our database, so we'll do user objects and then I can get ID
equal to one.
That's our user.
We also need to import users.
And we've done it already.
Here in the models.
So we'll copy that.
And I can.
Put it here.
So we have our user, if I will do user now and let's say we'd like to have username.
Send it again.
Coming back here, you can see Christian.
So we select the user based on the ID one that's a fixed ID, and then later on we'll replace with something
more precise.
So basically we could have another way to do it.
So whatever we would like to use this function, we can also request it to have that user ID in the
method itself.
So this one is kind of very robust.
So it will work for the login user.
But if we would like to allow other peoples who are not logging into our system to make us some kind
of rating, you will need to store it.
But we kind of design it this way that in the models we say, okay, you need to pass the user and that
need to be also unique together.
So user and movie.
So in the moment when you decided that you will pass the user, you need to actually create your application.
This way the user will be extracted from the from the person who is doing that call.
So if I'm logging now and I will try to do the rating, my user will be passed to that method.
And this method we have it here.
So it will later on will use this request user.
At the moment we have our static.
So what do we need to do here?
We have movie, we have stars and we have user.
So we have all the information we need to construct our rating.
So what we can do first is we have two options.
First, we will try to check if that rating already exists.
So if we have something in our database with that movie and stars, if we do have it, then we'll update
it.
If we don't have it, we create a new.
So in this way we'll have rate movie one method for two calls.
So it will be like our standard post and put in one method.
So let's try.
We can do try and here we'll try to select something from our database.
And I'm using keyword try here because otherwise if I will do this, might fail and it will break the
whole method.
But we don't want to break it.
We just want to check if we have already that in the database.
Otherwise we'll create create it.
So let's do rating, we'll name rating and then we'll try to select the rating objects and then we'll
try to get and get we need to do user is our user ID.
And then movie.
Will be equal to movie ID.
This user is the whole object.
This movie is the whole object.
So what we are trying to get here is the user is stored as a if we do here this model foreign key,
that's user ID, it's not the whole objects stored in a database.
So whenever we try to get here, something from here, we are saying this user needs to be equal as
user ID because that's what we store in database.
So ID are stored in the database.
Once we have that, we if we have something like a get, then we can do something about it.
So we can do rating and then we can do stars equal to and then we have our variable stars.
And then what we can do is rating and then we can do save.
And then we can create something if that fails.
So if there is no rating with that user and movie, this will fail and then we can catch it here and
then we can create that object from scratch.
So we do accept.
Like that.
And then we can use a similar function as we use here.
So I will copy most of this here.
So basically this time we'll do ratings objects.
Create.
So we will create in this time, instead of ID, we'll pass the whole objects because that will be done
by us.
And then we also need to do stars so stars will equal to stars.
So we are creating here a new object with this user movie and star.
So let's save it now.
And then we have.
We need to run it now.
That's all working fine.
And at the moment we still outputting this, but we can actually customize it.
It depends on what we have done.
So let's come back to the postman and send it here.
And this is invalid.
Later, you can see here we are trying to put whatever for the stars.
But as you know, we put that validator validators in place.
So we can't really put whatever here.
And you can see 500 internal server error.
So we need to put actual stars here.
It's a kind of hint that it's already our method is kind of working.
So if I will do two here, which is accepted acceptable value, I can send it now and you can see message
is working.
So what we can do here, I will go to another tab and I will do get method for the ratings and check
if we actually have something in our database.
Now I will send it.
And indeed we have.
So we have ID this movie one and then we have three stars, user and movie, and then we have for another
movie.
So this is movie two and this is movie one.
Here we try to do a movie two.
So if I will, let's say this, let's let's remember this one.
So because this rating will be for movie two and that's my user one.
And then we had stars two.
So if I will come back here and I will do now Stars five and send it again for the same method for the
same movie, let's come back here and see if that will change.
And indeed that's been changed.
So that means we updating the records, if I will go now and use this ID for rating.
So that will be two.
I will have only that rating for this one, but I would like to remove it now.
So I will click, delete and send it and this is gone.
So coming back to the list.
And we need to to get.
And you can see here I have only four movie won ratings.
So coming back here with the same I won't change anything here and send it again.
I'm expecting I will create that rating now.
It's working here.
And I will refresh it.
And you can see here it's working.
So our function if if the this rating doesn't exist for that movie, then it will be created.
If it exists already, it will update it.
So this is what we achieved here in our method.
So what else we can do?
Because at the moment we whatever we do here, we have our message like that.
But what we can do is we can output back the data that we passed in.
So I will make more space here and we can do a different things for try and we can do different things
for creating here.
So let's grab this.
I will put it here first, and then we'll do something so I can do here.
Either.
That's just a variable and then we can use a serializer to serialize the data.
I will do writing serializer here and I will put it there.
So writing Serializer the same way we've done it before, we can pass to this rating serializer what
we would like to serialize.
We have our rating object there, so I can serialize that.
I can also say that's many false.
So it's only one, uh, one rating and we have that data there.
So the message is we can change it for rating, update it, and then we can in this the same object
we can do, for example, result.
And then we can do serializer data.
So what we are doing here is we select the serializer with our updated rating when we save it.
Then we add more information into our response and we pass that response with the Http 200.
So I will just copy this and I will go here.
So this time we need to also apply this create into our variable.
So we will have our rating here that will be effect of creating our rating.
And then we serialize that data and then we can do rating created this time and we will also put it
there.
So coming back to the postman and let's try to come here and then I will do send.
You can see here, message rating updated and that was updated.
So I can do three here and the start three are here.
So if I will remove that this one because I would like to test how this the output will be when I create
something, I need to remove ID3.
So I'll do three and then change the method to.
Delete and then send it here and you can see nothing is there.
But if I will now try to, let's do four.
I'm expecting the message will be created and the starts will be four.
You can see writing created and the stars for.
So we have our method.
Here created.
So we cover a lot of things here, and I've done that in purpose because I would like to show you what
are the different possibilities when we have our own function.
So what we can do here is we can validate the request data.
We can select some objects from our database using this syntax that we talked about it before.
Then we can do different things based on the existence in the database and then we know how to output
something inside our.
Response.
So this is the object that we actually constructed on our own.
And this.
Is optional.
You don't need to do this.
And then it's all depends on how you would like to do things in our front end.
So if you require some kind of message for your user to be displayed like a rating created, you can
pass it as a message and then the result will be updated results or whatever you need to do.
You have full options here because all of that is our own code so we can decide it.
What will we put in the object?
What will be the message?
What will be the status and so on and so on.
So you can see here we've created a simple method of our own, but it will be very powerful because
we can have one method that will work for for either update or for post.