WEBVTT

00:01.300 --> 00:01.900
Okay.

00:01.900 --> 00:10.420
In this video we will be talking about filter options and what are the filter options in our model file?

00:10.420 --> 00:12.040
I can actually remove that comment.

00:12.040 --> 00:12.970
And now.

00:14.010 --> 00:19.690
So we have our model here, which is a book, and we have our one field.

00:19.710 --> 00:22.620
So the field options is everything.

00:22.620 --> 00:28.730
What we pass in that parenthesis at the moment we have max length, which is 36.

00:28.740 --> 00:37.500
I can remove it from now here and then I can pass some other parameters into that field and that all

00:37.740 --> 00:40.260
parameters are field options.

00:40.260 --> 00:42.320
So what options do we have?

00:42.330 --> 00:46.680
So let's go to the official Django documentation.

00:46.680 --> 00:56.730
If we go here we are on the page docs Django Project.com English to point to reference model field and

00:56.730 --> 00:58.650
we have model field reference.

00:58.650 --> 01:05.520
The first section is the field options and we have a bunch of different options that we can pass to

01:05.520 --> 01:12.150
our field and all of them are listed here and I will go through a few of them and I will show you the

01:12.150 --> 01:17.280
most popular and the one that we will use in our tutorial.

01:17.280 --> 01:24.060
So if you would like to know more about them, there is a good read about each of them here.

01:24.060 --> 01:29.850
So if you need something special, you can also refer to this list and find something that will be useful

01:29.850 --> 01:30.660
for you.

01:30.840 --> 01:37.080
Okay, so let's go back to our models here and let's try different options.

01:37.080 --> 01:46.950
So first, what we can pass here is we can pass null and then Null accepts the arguments of true or

01:46.950 --> 01:47.610
false.

01:48.480 --> 01:49.590
So it's a boolean.

01:50.760 --> 01:51.450
What does it mean?

01:51.450 --> 01:52.080
A null.

01:52.080 --> 01:53.130
So null.

01:53.160 --> 02:01.920
Basically, we are telling Django that if this field is empty then we should store null in the database.

02:01.920 --> 02:07.650
So this is purely a database field and it's not going to impact our validation.

02:07.650 --> 02:10.920
We have another field which is called blank.

02:12.750 --> 02:16.930
And then we also can pass true or false into it.

02:16.930 --> 02:20.680
Blank is a validation for our field.

02:20.680 --> 02:29.050
It means that if we have blank true means that we allow this field the title to be blank, which is

02:29.050 --> 02:29.770
empty.

02:30.040 --> 02:37.570
So if I will have blank true, we can create new book with no title if we'll have it to false.

02:38.470 --> 02:45.100
That means this title is required and we can't pass it really as empty.

02:45.100 --> 02:50.140
So I will leave it blank false here, which is by default like that.

02:50.140 --> 02:52.840
So another option what we can have.

02:52.840 --> 02:56.200
I will leave the null as well just for future references.

02:56.200 --> 02:57.130
So null.

02:57.130 --> 03:03.520
I will say true, but it doesn't really matter that much because if we have blank false and we don't

03:03.520 --> 03:07.900
allow this to be blank, then there is no way that field will be true.

03:07.900 --> 03:14.650
So I will leave it for reference here that we can use it, but in fact this will never happen together.

03:14.680 --> 03:19.300
Another option that I would like to show you is unique like that.

03:19.300 --> 03:22.510
And we also can pass true or false unique.

03:22.510 --> 03:31.030
I will pass it as true because unique means that if we will have multiple records in that table, each

03:31.030 --> 03:33.700
of these the title need to be unique.

03:33.700 --> 03:39.070
So as you remember from previous video, I add Hobbit book into our database.

03:39.070 --> 03:45.430
And if I would like to add another book with the same title, it will be prohibited.

03:45.760 --> 03:53.020
So you can't have two books with the same title in the database if you have that unique true.

03:53.050 --> 03:59.200
So basically you might have more than one field here and you can decide which one will be unique in

03:59.200 --> 04:00.250
our database.

04:01.260 --> 04:04.290
Another option will be default.

04:06.290 --> 04:06.910
Like that.

04:06.910 --> 04:09.310
And then this depends on the type.

04:09.400 --> 04:12.010
What we have here, it's a char field.

04:12.010 --> 04:14.560
That means it's a simple text or a string.

04:14.560 --> 04:19.840
So if we'd like to store a string, then we need to provide a default one.

04:19.840 --> 04:27.490
If we would like to have a default as empty here, then if we want to provide any data to this field,

04:27.520 --> 04:30.190
then the by default it will be stored like this.

04:30.190 --> 04:32.920
So basically this makes no sense with a null.

04:32.950 --> 04:33.460
True.

04:33.490 --> 04:38.080
Because if you have a null true then it will never hit this null.

04:38.110 --> 04:40.360
True because the default will be empty.

04:40.360 --> 04:43.570
And also blank is false here.

04:43.570 --> 04:45.310
That means it will be required.

04:45.310 --> 04:50.680
So the default will never will be triggered with a empty string here.

04:51.040 --> 04:55.560
So you need to kind of pick which one you would like to use it.

04:55.570 --> 05:01.180
I just put it all of them here just for reference and we'll remove it later on.

05:01.180 --> 05:09.140
But pay attention what you are putting here because that will rule the way you create your new records

05:09.140 --> 05:10.190
in a database.

05:10.850 --> 05:13.250
So another option I will go to the new line.

05:13.250 --> 05:15.140
Here is choices.

05:16.560 --> 05:23.000
And then we can decide what is acceptable values for this field.

05:23.010 --> 05:30.270
So what we can do is we can decide a set of different values that will be accepted and not accept of

05:30.270 --> 05:34.260
that values will be in this field acceptable.

05:34.260 --> 05:37.710
So let's create a new variable here and we call it.

05:39.630 --> 05:40.410
Like that.

05:40.860 --> 05:42.150
And that's our constant.

05:42.150 --> 05:49.460
So we put it all uppercase here, and then we'll open parentheses and inside we'll create a set.

05:49.470 --> 05:52.560
So it will be pair value.

05:52.560 --> 06:01.110
So let's do for example, HB like Hobbit, and then we might have another one.

06:13.740 --> 06:15.570
Lord of the Ring like that.

06:15.570 --> 06:21.870
So basically we have here a tuple which is a set of value pairs.

06:21.870 --> 06:26.940
So we have HP and Hobbit and then Lord and Lord of the Rings.

06:26.940 --> 06:35.910
So what we I can do here is I can pick these choices books and the only values that will be accepted

06:35.910 --> 06:39.960
in our books will be this value specified here.

06:40.290 --> 06:47.220
So there is no way we can put something else except of these values there.

06:47.610 --> 06:50.430
Maybe for a books and a title.

06:50.430 --> 06:56.340
It makes no sense to have choices like this, but we might have something like a, I don't know, a

06:56.340 --> 07:03.240
stars or something based on the numbers, and you might accept only certain things.

07:03.240 --> 07:10.620
Or for example, if you have a status like a maybe a order status, and then you might have something

07:10.620 --> 07:15.370
like let's say you can have it zero and then it will be.

07:18.180 --> 07:20.460
And then you might have another status.

07:32.710 --> 07:36.340
And then we can have, for example, like that.

07:38.130 --> 07:40.490
So we might have a different status.

07:40.500 --> 07:42.600
Let's call it status.

07:44.440 --> 07:52.000
And then we might have it here for different let's say we are having our store and then we have a status

07:52.000 --> 07:53.770
status for orders.

07:53.770 --> 08:01.660
So we might accept only zero 1 or 2 in our database and zero will represent unknown status and then

08:01.660 --> 08:06.160
one will be processed and the second one will be paid or wherever you will have it here.

08:06.160 --> 08:15.880
But the the key here is that choices will restrict only to these values from the array that we set up

08:15.880 --> 08:16.720
in here.

08:16.720 --> 08:24.010
So that's sometimes is very handy in our case, in case of title as we have it here, this makes no

08:24.010 --> 08:29.290
sense because we like to us add as many book titles as we want.

08:29.290 --> 08:32.710
And in our case, what makes sense is.

08:33.970 --> 08:36.100
We can remove the default one.

08:36.130 --> 08:38.140
We can also remove the choices.

08:38.170 --> 08:45.010
We can leave unique as true because we don't want to have multiple books with the same title in our

08:45.010 --> 08:45.850
database.

08:45.850 --> 08:48.220
And then we'll do blank False.

08:48.670 --> 08:57.460
That means it will be required field and it needs to be unique so I can remove this and whatever I change

08:57.460 --> 08:58.390
in this.

08:59.500 --> 09:00.260
Field.

09:00.370 --> 09:02.200
I will save the changes here.

09:02.200 --> 09:07.960
I can open the terminal and I can do Python three Manage.py.

09:09.240 --> 09:11.580
Make migrations.

09:14.300 --> 09:17.710
And you can see here, I forgot to add the max length.

09:17.720 --> 09:20.310
The char field must define the max length.

09:20.340 --> 09:23.720
I had it before, but I remove it to show you the other attributes.

09:23.720 --> 09:29.000
So we will come back to this and we'll do 36 as we had it there.

09:29.000 --> 09:32.870
So Max, length of this char field will be 36.

09:32.870 --> 09:35.630
And then we decided blank, true and unique.

09:35.630 --> 09:36.110
True.

09:36.110 --> 09:42.530
So I will save this now and then I will do make migrations and you can see Alter Fields title on the

09:42.530 --> 09:42.860
book.

09:42.860 --> 09:50.120
So whatever we do some changes in our field, we need to register that and set the database to accept

09:50.120 --> 09:51.500
these values here.

09:51.710 --> 09:59.450
So if this is required, required a unique the database will be set up for this fields like that.

09:59.630 --> 10:07.340
So once we have this, you can see migrations, we have another file and this file is basically changing

10:07.340 --> 10:09.800
this with some setup.

10:09.800 --> 10:13.680
So we'll close it, but it's not hitting the database yet.

10:13.680 --> 10:19.530
So as you remember to apply to the database, we need to use command migrate.

10:21.380 --> 10:25.340
And we have our migration applied to this.

10:25.370 --> 10:29.120
We also have some admin and authorization changes here.

10:29.120 --> 10:35.740
We saw a prompt from the Django about those that hasn't been applied yet and now they have been applied

10:35.750 --> 10:38.090
all the missing before migrations.

10:38.090 --> 10:40.910
And also the last one is Demo 002.

10:40.940 --> 10:44.180
This is the changes we just made on this field.

10:44.360 --> 10:50.390
In the next video, I will show you what kind of different field types we have.
