WEBVTT

00:01.510 --> 00:02.140
Okay.

00:02.140 --> 00:06.850
In this video, we will create our first models in our application.

00:06.850 --> 00:12.010
So let's open a models.py here and we can create models here.

00:12.220 --> 00:17.020
As you remember, the way to create a model is class and the name of the model.

00:17.020 --> 00:19.780
What we what models do we need in our application?

00:19.780 --> 00:27.580
Basically, based on our requirements, we will need to have a list of movies and also will rate that

00:27.580 --> 00:27.940
movie.

00:27.940 --> 00:29.650
So we will need two models.

00:29.650 --> 00:31.090
First will be movie.

00:32.990 --> 00:34.640
So we can do models.

00:37.730 --> 00:42.920
Model like that and then we can have a title.

00:44.880 --> 00:46.810
This field will be models.

00:48.970 --> 00:50.010
Char Field.

00:50.940 --> 00:53.340
And let's say max length.

00:53.370 --> 00:57.330
I think for a movie title will be 32 will be more than enough.

00:57.330 --> 00:59.430
And then we can have a description.

01:01.920 --> 01:03.660
And we can do models.

01:03.900 --> 01:04.980
Models.

01:06.920 --> 01:12.020
And on this one we can do text field because that will be much larger.

01:12.020 --> 01:13.850
So we can do max length.

01:13.850 --> 01:21.140
Let's say we can have a 360 character and just an arbitrary number here.

01:21.140 --> 01:24.830
So what we have is we have our movie here.

01:24.830 --> 01:32.060
We also need to create a new class, new model for our rating like that.

01:32.060 --> 01:36.650
And in the same way so we models model, import it.

01:36.650 --> 01:40.670
And here what we can do is we will have few things.

01:40.670 --> 01:43.820
First, we need to have a reference to our movie.

01:43.820 --> 01:53.630
So our movie will be models foreign key, and then we'll refer our movie here.

01:55.190 --> 01:59.080
So we will have a reference from the rating to movies.

01:59.080 --> 02:03.650
So whatever we'll create a rating, we'll need to create a for specific movie.

02:04.160 --> 02:10.550
Another I can actually duplicate this and I will have a reference to user.

02:10.580 --> 02:17.810
The reason why I'm having this is I will kind of explain you and show you that in a second.

02:17.810 --> 02:22.280
And once we will start to test our API, it will make more sense.

02:22.280 --> 02:31.490
But what we want to have is if we create a rating for our movie, we will have a reference to the movie.

02:31.490 --> 02:38.900
So this rating will be for specific movie, but also we'll need to also tell what user created that

02:39.320 --> 02:40.070
rating.

02:40.070 --> 02:49.640
So basically if we have a login user, that user will be sent with the request and we need to store

02:49.640 --> 02:55.860
that user so user won't be able to create million reference ratings for one movie.

02:55.860 --> 02:59.430
So one rating per movie per user.

02:59.430 --> 03:07.110
So this in this way we need to reference the user, but a user is not available here, so we need to

03:07.110 --> 03:07.920
import it.

03:07.920 --> 03:14.700
As you can see here, the class a user doesn't exist, but we can import it actually from Django.

03:14.700 --> 03:17.460
So from Django.

03:19.680 --> 03:20.760
Contrib.

03:22.790 --> 03:27.230
Out models import user.

03:27.530 --> 03:35.000
So we have our user imported from the Django contrib authentication models that's built in model inside

03:35.030 --> 03:35.480
Django.

03:35.480 --> 03:43.040
As we can go here, you can see users here, that's a user built in in authentication.

03:43.040 --> 03:49.940
And what we are doing here is we import that user from, from Django itself and now we have two references.

03:49.940 --> 03:53.390
So we have movie and we also have that user.

03:53.390 --> 04:03.430
What we also need to do is do on delete and then we'll do models, cascades.

04:03.560 --> 04:04.550
Cascade.

04:05.240 --> 04:07.400
Cascade like that.

04:07.700 --> 04:09.590
And that will do it for both.

04:09.830 --> 04:19.340
So basically what I'm saying here is if we remove that movie, we also need to remove that rating.

04:19.340 --> 04:28.320
So if we remove that model, then we will cascade it and remove the rating because if we will have a

04:28.320 --> 04:34.410
rating with that movie and the movie will be removed from our database, it will break our application,

04:34.410 --> 04:35.670
it will break the logic.

04:35.670 --> 04:41.370
So whatever this model will be removed, we cascade it and remove the rating as well.

04:41.790 --> 04:49.380
So we have two references now, but it will be good to also have some kind of rating so we can call

04:49.380 --> 04:50.400
it stars.

04:51.540 --> 04:58.650
So we creating our rating, we will tell what is the number of stars.

04:58.650 --> 05:02.400
We would like to give that to our model.

05:02.400 --> 05:09.750
So what we can do is we can use models and then we can do integer field.

05:09.750 --> 05:13.920
So Integer Field will store the integer for us.

05:13.920 --> 05:22.050
But how can we actually tell Django that we would like to have only from 1 to 5 stars?

05:22.050 --> 05:23.760
We can use validators.

05:23.760 --> 05:31.050
I haven't talked about the validator validators yet, but it is something available in the Django and

05:31.050 --> 05:33.840
we can import that and use it in the field.

05:33.840 --> 05:34.920
So from.

05:36.850 --> 05:41.110
Django core validators.

05:41.710 --> 05:44.180
We can import max length.

05:44.710 --> 05:51.670
Max value validator and also we can import minimum value validator.

05:51.670 --> 06:02.230
So we have to validate those here and inside here we can actually use the validators in the field and

06:02.230 --> 06:09.520
we can specify what will be the minimum value for this integer and the maximum value for this integer.

06:09.520 --> 06:14.710
So what we need to do is we need to type validators here and that's going to be an array.

06:14.710 --> 06:18.310
So we do square brackets here and then we can decide it.

06:18.310 --> 06:21.490
So minimum valid value validator.

06:21.490 --> 06:27.730
And then we we do parentheses and then we can decide a minimum validator is one.

06:27.730 --> 06:37.190
So I do comma here and then I will do max value validator here inside that square brackets and then

06:37.190 --> 06:40.160
I can decide this max value will be five.

06:40.190 --> 06:47.300
So basically we are taking that stars it's an integer field so the whole number and then validators

06:47.300 --> 06:50.750
will use minimum value one and then max value five.

06:50.780 --> 06:55.040
So we can only accept values one, two, three, four and five.

06:55.070 --> 07:02.780
So that will be a very good for our stars because we don't want to have a user to pass any value he

07:02.780 --> 07:03.950
likes in the stars.

07:03.950 --> 07:08.390
So we'll put it concise and it will be very useful.

07:09.110 --> 07:20.480
Also, another thing that I haven't talked about it yet is unique together, so we know how to do unique

07:20.480 --> 07:25.310
things like we can decide that this field will be unique or this field will be unique.

07:25.310 --> 07:29.210
But I haven't talk yet about unique together.

07:29.210 --> 07:38.120
If we do class meta like this, what we can decide for this rating is we can decide.

07:45.640 --> 07:47.170
On unique together.

07:47.290 --> 07:55.660
We need to do brackets like this and inside another I will do our user.

07:59.860 --> 08:01.390
And movie.

08:02.290 --> 08:08.900
And also I can do index together in the same way.

08:08.920 --> 08:18.040
So basically what I'm saying here that if you create a rating, the only accepted values will be if

08:18.040 --> 08:22.360
the user and movie won't be already in our database.

08:22.360 --> 08:28.450
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

08:28.450 --> 08:30.300
example how we've done this.

08:30.310 --> 08:39.970
So basically what we are saying is if we have already rating for a specific movie by one user and if

08:39.970 --> 08:47.320
you would like to create a new rating for the same movie with the same user, it will be rejected because

08:47.320 --> 08:49.690
we have that unique together here.

08:49.870 --> 08:53.650
And also we do index together in the same way.

08:53.650 --> 09:05.480
So that's a setup for this model that will help us to do not accept any values that won't be acceptable

09:05.480 --> 09:06.170
for us.

09:06.170 --> 09:13.070
So we want to design this in a way that will work and it will be bulletproof and this is the way to

09:13.070 --> 09:13.460
do it.

09:13.460 --> 09:15.800
So I will save it now.

09:15.800 --> 09:19.970
And you can see here we have two classes, two, two models.

09:19.970 --> 09:26.300
Now what do we need to do is we need to create migrations and we need to apply it in our database.

09:26.300 --> 09:27.560
So let's do it now.

09:27.560 --> 09:30.290
So I will go Python.

09:32.510 --> 09:35.750
Manage.py make migrations.

09:39.820 --> 09:41.400
Supposed to be Python.

09:46.030 --> 09:47.740
And another typo.

09:49.150 --> 09:50.920
Manage.py make migrations.

09:50.920 --> 09:56.590
And you can see here two models has been created movie and rating.

09:56.590 --> 10:05.110
So what we need to do is we need to migrate it now, migrate like this and that's been created.

10:05.110 --> 10:11.080
So I will go to admin and I can register these two models here.

10:11.080 --> 10:13.240
So we go admin.

10:14.110 --> 10:16.660
Site register.

10:19.440 --> 10:26.820
And then model is one is movie and I will duplicate it and another one is rating.

10:27.060 --> 10:29.790
We also need to import it.

10:29.790 --> 10:30.870
So from.

10:32.200 --> 10:39.550
Models import movie and writing like that.

10:39.550 --> 10:41.650
So we have that done.

10:41.650 --> 10:50.050
And then if I go here and I will refresh it so it cannot be reached, probably we don't have our server

10:50.050 --> 10:51.640
running, so I go run.

10:52.840 --> 10:53.920
Click run here.

10:56.190 --> 10:58.830
Coming back here and then we'll refresh it.

10:59.460 --> 11:03.210
And you can see here movies and ratings are here.

11:03.210 --> 11:09.750
I can click here and if I will add new movie, it is available title and description.

11:09.750 --> 11:16.500
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

11:16.500 --> 11:23.130
any and then we have users, we have one user in our database and then we can do a number of stars.

11:23.130 --> 11:32.190
So if I will pick here, actually this, this field has no validation, but the Django will refuse it.

11:32.190 --> 11:38.220
If I will try to pass something different, then our validation there.

11:38.760 --> 11:41.190
So we have basic models set up.

11:41.190 --> 11:46.440
We haven't do anything with Serializers yet and we haven't done anything with views.

11:46.440 --> 11:52.770
So we'll focus on that in the next video and I will start testing our API.

11:52.800 --> 12:00.550
At the moment we have everything configured so we could add it, our models, our records in database

12:00.550 --> 12:08.560
in this admin section, but our goal is to use the API and return the Json.

12:08.560 --> 12:15.940
So we will try to debug it and make it work with our Json using the postman.
