Okay.
In this video, we will create our first models in our application.
So let's open a models.py here and we can create models here.
As you remember, the way to create a model is class and the name of the model.
What we what models do we need in our application?
Basically, based on our requirements, we will need to have a list of movies and also will rate that
movie.
So we will need two models.
First will be movie.
So we can do models.
Model like that and then we can have a title.
This field will be models.
Char Field.
And let's say max length.
I think for a movie title will be 32 will be more than enough.
And then we can have a description.
And we can do models.
Models.
And on this one we can do text field because that will be much larger.
So we can do max length.
Let's say we can have a 360 character and just an arbitrary number here.
So what we have is we have our movie here.
We also need to create a new class, new model for our rating like that.
And in the same way so we models model, import it.
And here what we can do is we will have few things.
First, we need to have a reference to our movie.
So our movie will be models foreign key, and then we'll refer our movie here.
So we will have a reference from the rating to movies.
So whatever we'll create a rating, we'll need to create a for specific movie.
Another I can actually duplicate this and I will have a reference to user.
The reason why I'm having this is I will kind of explain you and show you that in a second.
And once we will start to test our API, it will make more sense.
But what we want to have is if we create a rating for our movie, we will have a reference to the movie.
So this rating will be for specific movie, but also we'll need to also tell what user created that
rating.
So basically if we have a login user, that user will be sent with the request and we need to store
that user so user won't be able to create million reference ratings for one movie.
So one rating per movie per user.
So this in this way we need to reference the user, but a user is not available here, so we need to
import it.
As you can see here, the class a user doesn't exist, but we can import it actually from Django.
So from Django.
Contrib.
Out models import user.
So we have our user imported from the Django contrib authentication models that's built in model inside
Django.
As we can go here, you can see users here, that's a user built in in authentication.
And what we are doing here is we import that user from, from Django itself and now we have two references.
So we have movie and we also have that user.
What we also need to do is do on delete and then we'll do models, cascades.
Cascade.
Cascade like that.
And that will do it for both.
So basically what I'm saying here is if we remove that movie, we also need to remove that rating.
So if we remove that model, then we will cascade it and remove the rating because if we will have a
rating with that movie and the movie will be removed from our database, it will break our application,
it will break the logic.
So whatever this model will be removed, we cascade it and remove the rating as well.
So we have two references now, but it will be good to also have some kind of rating so we can call
it stars.
So we creating our rating, we will tell what is the number of stars.
We would like to give that to our model.
So what we can do is we can use models and then we can do integer field.
So Integer Field will store the integer for us.
But how can we actually tell Django that we would like to have only from 1 to 5 stars?
We can use validators.
I haven't talked about the validator validators yet, but it is something available in the Django and
we can import that and use it in the field.
So from.
Django core validators.
We can import max length.
Max value validator and also we can import minimum value validator.
So we have to validate those here and inside here we can actually use the validators in the field and
we can specify what will be the minimum value for this integer and the maximum value for this integer.
So what we need to do is we need to type validators here and that's going to be an array.
So we do square brackets here and then we can decide it.
So minimum valid value validator.
And then we we do parentheses and then we can decide a minimum validator is one.
So I do comma here and then I will do max value validator here inside that square brackets and then
I can decide this max value will be five.
So basically we are taking that stars it's an integer field so the whole number and then validators
will use minimum value one and then max value five.
So we can only accept values one, two, three, four and five.
So that will be a very good for our stars because we don't want to have a user to pass any value he
likes in the stars.
So we'll put it concise and it will be very useful.
Also, another thing that I haven't talked about it yet is unique together, so we know how to do unique
things like we can decide that this field will be unique or this field will be unique.
But I haven't talk yet about unique together.
If we do class meta like this, what we can decide for this rating is we can decide.
On unique together.
We need to do brackets like this and inside another I will do our user.
And movie.
And also I can do index together in the same way.
So basically what I'm saying here that if you create a rating, the only accepted values will be if
the user and movie won't be already in our database.
So let's say we have one movie and I will go to the testing later on and I will show you that on the
example how we've done this.
So basically what we are saying is if we have already rating for a specific movie by one user and if
you would like to create a new rating for the same movie with the same user, it will be rejected because
we have that unique together here.
And also we do index together in the same way.
So that's a setup for this model that will help us to do not accept any values that won't be acceptable
for us.
So we want to design this in a way that will work and it will be bulletproof and this is the way to
do it.
So I will save it now.
And you can see here we have two classes, two, two models.
Now what do we need to do is we need to create migrations and we need to apply it in our database.
So let's do it now.
So I will go Python.
Manage.py make migrations.
Supposed to be Python.
And another typo.
Manage.py make migrations.
And you can see here two models has been created movie and rating.
So what we need to do is we need to migrate it now, migrate like this and that's been created.
So I will go to admin and I can register these two models here.
So we go admin.
Site register.
And then model is one is movie and I will duplicate it and another one is rating.
We also need to import it.
So from.
Models import movie and writing like that.
So we have that done.
And then if I go here and I will refresh it so it cannot be reached, probably we don't have our server
running, so I go run.
Click run here.
Coming back here and then we'll refresh it.
And you can see here movies and ratings are here.
I can click here and if I will add new movie, it is available title and description.
If I go back to API and then ratings, if I will try to add, we have movie at the moment we don't have
any and then we have users, we have one user in our database and then we can do a number of stars.
So if I will pick here, actually this, this field has no validation, but the Django will refuse it.
If I will try to pass something different, then our validation there.
So we have basic models set up.
We haven't do anything with Serializers yet and we haven't done anything with views.
So we'll focus on that in the next video and I will start testing our API.
At the moment we have everything configured so we could add it, our models, our records in database
in this admin section, but our goal is to use the API and return the Json.
So we will try to debug it and make it work with our Json using the postman.