Okay.
In this video, we will be talking about the field types available in Django.
We are on the same page as we were in the previous video, which is model field reference in the official
Django documentation.
And if you scroll a little bit down, you can see here you have a field types and the list of these
types is quite long.
We are not going to go through all of them, but I will show you a few main ones in our application.
So let's go back to our code here.
And the first type I would like to told you about is the char field.
So this is what we use.
That's a simple text field.
So Char Field is designed to store our text like a title.
And then another option, what we have to store the text is a text field.
So I will do here and then I will do description.
And then I can do models like we import here and on the models we have all that types different available
for us.
So I will do text field here and then what we can do, we can also provide max length.
Max length, let's say 256 for this and then we can do blank True.
And I will show you in a in a second and then unique might be false, but by default it is false.
So what what is happening here?
The text field is also storing a text, but it's also designed for storing a larger text.
The char field is supposed to be a short and text like like the title and then we can have some text.
The description in our case of a book might be a very long and then we'll use the text field, which
is the text field is designed for storing a much larger piece of the text, like the description, for
example, or article or whatever.
You need to store it as a long text.
This doesn't need to be 256 I just put our example here that this max length will be 36, which is shorter
and the longer will be 256 but this might be any number you like.
And then we put a blank True here that means we allow description to be blank, whatever we will.
User description, we might add it or not, and blank decide is that is not required.
Field.
Okay, so we have a text store here.
Another example of a of this, we can actually store numbers and then we have a few options for numbers
and I will show you a few of them.
So first thing will be, let's say price.
Models and then we can do integer field and then we have integer field.
Let's say default will be zero and then price will store the number as a integer field, but price might
be with cent, so we might not be integer.
What other options we have for numbers?
We have few of them like a big integer.
Like this one.
So integer they have some maximum numbers that we can store it in the memory if we'd like to store and
be sure that this integer might be a very long integer, then we will use the big integer.
But it's also not a good case for our price because the price might have some decimal numbers.
So what we can use is we can use decimal field for that decimal field.
We can actually store some decimal fields and available options for us is.
Max digit.
Digits and will put zero for now.
Decimal places.
I will put a zero for now.
So what we have here the decimal will store also the the integer along with some decimal places so we
can decide here how many decimal places we'd like to have for this field.
Like two for cents and the max digits.
We also might decide how many max digits we want to have it for this decimal and this field will be
stored as a decimal.
Another option will be float.
So basically the difference between the float and the decimal is that it will be stored in a jungle
as a float or as a decimal object in our python.
So that's for that's for our field.
With the numbers, we have a few different options and whatever you have, you need to.
Store some number, just pick the type that will the best represent the type of the number that you
would like to store.
So what else?
Let's say we have a date and we can have published and we will store when the book has been published
so we can do models and then we can decide what kind of field we'll use for this.
So date field will represent a date in our database.
And you can see here from the from this tooltip that we might add extra options to it so we can do auto
now for true.
That means whatever we will save that book.
We will set this field as a current date.
Also we have another option auto now add true.
So you have two options for each of the date field for each field that will represent the date and time.
So auto now true is wherever you will save this model this.
Book, then it will set to the current date.
If you have auto now add, that means it will be only set when you create new.
So let's say I will create a new book and I will have it this set up to auto.
Now add.
This will be set up for a timestamp that the book has been created.
This one will always whatever we will save our book which is creating new or updating it will set a
new timestamp for this.
So we have that options here.
But also we have date or we can have time.
Time field will store the time.
So maybe in case of published it makes no sense because there is no time.
It will be date.
But we also have an option for time.
So if you would like to store time in our database, this will be the fields use third option.
We have date time, which is date and time in one field.
So if you are would like to store exactly timestamp when something happened and that will be year,
month, day hours, minutes and seconds that will be your field to pick.
So if you need to know when something happened in a very precision date, time field is the option.
So all we reverted to date field and I will remove that auto because if the book is published, we don't
want to use any of this auto that is more for storing when the record has been created, but we are
not interested in that.
Another field that we might use will be a file field.
So we'll do cover and then models and we'll do file field like that.
So basically this file field will allow us to add a file for this specific book and what we can provide
here, it's upload to and then we can decide if we would like to have it inside folder of covers like
that.
So what I'm saying here is this will be a file, it will accept the files and that will be covered for
our book and then we can store it inside the covers folder.
So we'll have a covers folder here and it will be stored there.
So the file we can decide any file.
It also have to be image file.
It will accept any file, but we have another option.
We can go image file field like that.
The image field is pretty much the same as a file field, but this time it's accept images and we can
provide extra height.
Height field and also width for the image, but you can use both to store your covers if you like.
You can also have that upload to covers.
So that's pretty much it for this kind of types.
I haven't cover all of them.
So you can see here you have much more options, like for example, you have.
Email field if we want to store the email and so on and so on.
You have another one boolean, which is simply storing.
True or false?
So if you would like to have it.
For example.
His published.
And then models
Boolean field.
And then you can do default.
False.
So you might have a field that will accept true or false values only.
So it will be zero for false and true one for true.
Then you can use the Boolean field is published and then you can switch it from yes to no.
So the default we can set default defaults to false.
So I can save it now and I have a few of them here ready for for me.
So I've done some changes.
Now I will need to do make migration.
So I'll do make migrations here again, I press arrow up if you press arrow up here once you are in
the terminal, you will select the previous command arrow up again and two previous commands.
So make migrations.
And we have some problems.
The problem we see here is the image field image field we have here.
So Image Field in Django use a project called called Pillow.
So we need to install that pillow to use that image field.
And you can see here the command for for this will be pip install pillow.
So we can do that pip install pillow.
Pillow has been added so we can add it.
Make migrations again.
And you can see here we have some errors and that will be validating every time you will do my migrations
for you to not make any mistake.
And you see that small fields must defined marks digits so marks digit.
I can have three.
Make migrations.
And we have another option.
So you're trying to add a non-nullable field cover to book without a default.
So that means the cover and there is no default for the cover and then it cannot be blank.
But what we can do here is provide a one off default for the existing books we have.
At the moment.
We have only one book which is Hobbit, and Django doesn't know what to do with that book because we
have a cover and it cannot be empty.
So what we can do is provide one off or quit this and deal with that on our own.
So I will do the to quit now and then I can do here blank.
True.
That means it's not required.
So we can skip it the cover if we don't have it.
So I will try once again.
And we have another one with published.
So published here we need to provide.
We can actually specify it might be blank.
So let's take a look.
We have this one sorted description might be blank.
Then we have price, we have default.
So that's covered.
The publish might be blank is published.
We have default false.
So it will be set like this and then cover might be blank.
So we should be okay for now.
So we'll do two here to escape this question and then I will try make migrations again.
And then we have published.
I think I need to save this.
And then do two and then try it again.
And let me take a look.
Publish a book without a default.
So we have published, but it's blank.
True.
So it should be okay.
And it is not to book without a default.
We can't do that.
Okay.
Uh, the the problem we see here is that we accept blank.
True.
That means for a new record, for new book, it can be empty.
But in fact, we have something in our database, and Django doesn't know what to do with this.
So we can do null.
True.
So everything will be by default.
Store it as true and then we can have default none.
I can save it here.
So select an option to and I will try once again.
And you can see here I've dealt with all the conflicts we have.
If you design your database without any records in your database, you won't have that problems.
But if you have something already there, you need to make sure you will cover all the cases for each
field and Django will know what to do with the existing records.
So we've done that.
And you can see we add few fields into our database.
Next step will be to apply that migrations with a command migrate.
You can see that has been added.
The new file has been added to the database.
So let's click quickly.
Go.
To our.
Server.
And then we'll go to admin.
And I will go to the books and I can select our hobbit.
And you can see here we have much more options now.
So we have Hobbit, which is a short char field.
Then we have our text field here, which is much longer, as you can see here.
Then we have our decimal price, then we have publish, which is kind of type of date.
So if I will select date, you can see the date is selected or I can use this widget for a calendar
and then I have Boolean which is, is published and then I have a cover which I can choose a file and
attach the file for this.
So you can see each of this type that we specified in the model will have a different input method.
So the input method admin will automatically pick up input method based on the type we've chosen in
our model field.
So as I said before, we have extra options here and there is a lot of different types.
So whatever you need to store something in your database, just go to this page and try to find the
the one that best suits your needs.