Okay.
In this video we will be talking about filter options and what are the filter options in our model file?
I can actually remove that comment.
And now.
So we have our model here, which is a book, and we have our one field.
So the field options is everything.
What we pass in that parenthesis at the moment we have max length, which is 36.
I can remove it from now here and then I can pass some other parameters into that field and that all
parameters are field options.
So what options do we have?
So let's go to the official Django documentation.
If we go here we are on the page docs Django Project.com English to point to reference model field and
we have model field reference.
The first section is the field options and we have a bunch of different options that we can pass to
our field and all of them are listed here and I will go through a few of them and I will show you the
most popular and the one that we will use in our tutorial.
So if you would like to know more about them, there is a good read about each of them here.
So if you need something special, you can also refer to this list and find something that will be useful
for you.
Okay, so let's go back to our models here and let's try different options.
So first, what we can pass here is we can pass null and then Null accepts the arguments of true or
false.
So it's a boolean.
What does it mean?
A null.
So null.
Basically, we are telling Django that if this field is empty then we should store null in the database.
So this is purely a database field and it's not going to impact our validation.
We have another field which is called blank.
And then we also can pass true or false into it.
Blank is a validation for our field.
It means that if we have blank true means that we allow this field the title to be blank, which is
empty.
So if I will have blank true, we can create new book with no title if we'll have it to false.
That means this title is required and we can't pass it really as empty.
So I will leave it blank false here, which is by default like that.
So another option what we can have.
I will leave the null as well just for future references.
So null.
I will say true, but it doesn't really matter that much because if we have blank false and we don't
allow this to be blank, then there is no way that field will be true.
So I will leave it for reference here that we can use it, but in fact this will never happen together.
Another option that I would like to show you is unique like that.
And we also can pass true or false unique.
I will pass it as true because unique means that if we will have multiple records in that table, each
of these the title need to be unique.
So as you remember from previous video, I add Hobbit book into our database.
And if I would like to add another book with the same title, it will be prohibited.
So you can't have two books with the same title in the database if you have that unique true.
So basically you might have more than one field here and you can decide which one will be unique in
our database.
Another option will be default.
Like that.
And then this depends on the type.
What we have here, it's a char field.
That means it's a simple text or a string.
So if we'd like to store a string, then we need to provide a default one.
If we would like to have a default as empty here, then if we want to provide any data to this field,
then the by default it will be stored like this.
So basically this makes no sense with a null.
True.
Because if you have a null true then it will never hit this null.
True because the default will be empty.
And also blank is false here.
That means it will be required.
So the default will never will be triggered with a empty string here.
So you need to kind of pick which one you would like to use it.
I just put it all of them here just for reference and we'll remove it later on.
But pay attention what you are putting here because that will rule the way you create your new records
in a database.
So another option I will go to the new line.
Here is choices.
And then we can decide what is acceptable values for this field.
So what we can do is we can decide a set of different values that will be accepted and not accept of
that values will be in this field acceptable.
So let's create a new variable here and we call it.
Like that.
And that's our constant.
So we put it all uppercase here, and then we'll open parentheses and inside we'll create a set.
So it will be pair value.
So let's do for example, HB like Hobbit, and then we might have another one.
Lord of the Ring like that.
So basically we have here a tuple which is a set of value pairs.
So we have HP and Hobbit and then Lord and Lord of the Rings.
So what we I can do here is I can pick these choices books and the only values that will be accepted
in our books will be this value specified here.
So there is no way we can put something else except of these values there.
Maybe for a books and a title.
It makes no sense to have choices like this, but we might have something like a, I don't know, a
stars or something based on the numbers, and you might accept only certain things.
Or for example, if you have a status like a maybe a order status, and then you might have something
like let's say you can have it zero and then it will be.
And then you might have another status.
And then we can have, for example, like that.
So we might have a different status.
Let's call it status.
And then we might have it here for different let's say we are having our store and then we have a status
status for orders.
So we might accept only zero 1 or 2 in our database and zero will represent unknown status and then
one will be processed and the second one will be paid or wherever you will have it here.
But the the key here is that choices will restrict only to these values from the array that we set up
in here.
So that's sometimes is very handy in our case, in case of title as we have it here, this makes no
sense because we like to us add as many book titles as we want.
And in our case, what makes sense is.
We can remove the default one.
We can also remove the choices.
We can leave unique as true because we don't want to have multiple books with the same title in our
database.
And then we'll do blank False.
That means it will be required field and it needs to be unique so I can remove this and whatever I change
in this.
Field.
I will save the changes here.
I can open the terminal and I can do Python three Manage.py.
Make migrations.
And you can see here, I forgot to add the max length.
The char field must define the max length.
I had it before, but I remove it to show you the other attributes.
So we will come back to this and we'll do 36 as we had it there.
So Max, length of this char field will be 36.
And then we decided blank, true and unique.
True.
So I will save this now and then I will do make migrations and you can see Alter Fields title on the
book.
So whatever we do some changes in our field, we need to register that and set the database to accept
these values here.
So if this is required, required a unique the database will be set up for this fields like that.
So once we have this, you can see migrations, we have another file and this file is basically changing
this with some setup.
So we'll close it, but it's not hitting the database yet.
So as you remember to apply to the database, we need to use command migrate.
And we have our migration applied to this.
We also have some admin and authorization changes here.
We saw a prompt from the Django about those that hasn't been applied yet and now they have been applied
all the missing before migrations.
And also the last one is Demo 002.
This is the changes we just made on this field.
In the next video, I will show you what kind of different field types we have.