WEBVTT

00:00.880 --> 00:01.480
Okay.

00:01.690 --> 00:10.210
So far we display a dummy text here in our web application, but it has nothing to do with what we have

00:10.210 --> 00:13.650
in our Django, which is inside our models.

00:13.660 --> 00:18.460
We have something that we can actually use and that's coming from the database.

00:18.460 --> 00:22.360
So we seen that book already in the admin page.

00:22.360 --> 00:26.620
And let's go to the admin and let's do something there.

00:27.130 --> 00:28.540
So if I go admin.

00:31.570 --> 00:33.460
I have just one book here.

00:33.460 --> 00:34.960
I can add another one.

00:35.770 --> 00:45.780
Lord of the Ring or whatever some description goes here and so on.

00:45.790 --> 00:51.790
The price, I can have it for one and publish I can do it today is publish.

00:51.790 --> 00:52.390
It is.

00:52.420 --> 00:54.610
And then I will just leave it there.

00:54.610 --> 00:57.940
I'll make it a bigger screen and I will save it.

00:57.940 --> 00:59.470
So we have two books.

00:59.470 --> 01:06.010
One book is Hobbit and another book is Lord of the Ring.

01:06.010 --> 01:08.680
So that's what we have in our database now.

01:08.680 --> 01:13.750
So let's come back to our application here and goes to the views.

01:13.750 --> 01:20.530
And what we can do actually is we can use whatever is in our database and we can do something about

01:20.530 --> 01:22.540
it anywhere in our application.

01:22.540 --> 01:27.280
It doesn't have to be views, but views is a very good candidate to use it because we can actually grab

01:27.280 --> 01:33.620
something from our database and push it to our view, which is displayed on the screen for the user

01:33.620 --> 01:34.580
and what we can do.

01:34.580 --> 01:36.380
First, we need to import.

01:36.380 --> 01:42.920
So from dot models, dot always means from the current folder, we are inside the views.

01:42.950 --> 01:48.980
As you can see here, model synthesis is in the same folder, so we'll import from the models and then

01:48.980 --> 01:50.810
we'll import book.

01:50.810 --> 01:54.500
So the book is available for us now.

01:54.590 --> 02:03.650
And then the book was just a this class here that has all of this included.

02:03.650 --> 02:09.350
But what we can do here is we can actually reference some some books here.

02:09.350 --> 02:10.670
So I will do books.

02:11.590 --> 02:14.140
And then it will be equal to something.

02:14.140 --> 02:25.480
And in Django, we can reference the model like Doc, which is name of the class book and then objects.

02:28.290 --> 02:31.680
And then we can do, for example, all.

02:32.130 --> 02:33.350
So what does it mean?

02:33.360 --> 02:41.850
We're taking books, our model, we reference all objects of that model and then we want to select all

02:41.850 --> 02:43.830
of them and books now.

02:43.860 --> 02:47.160
Now they represent the set of our books.

02:47.160 --> 02:52.430
So what we can do here is we can actually create a new string.

02:52.440 --> 03:01.080
So output, I can name it whatever I like, but I will do a format here, which is just a formatting

03:01.080 --> 03:04.050
the string and we'll say we have.

03:04.940 --> 03:11.120
And then that many books in database.

03:11.690 --> 03:16.850
So what's happening here is actually I format the string.

03:16.850 --> 03:25.400
And inside here what I can do is I can inject length, which is length of our array of books.

03:25.400 --> 03:31.040
And we can we will be able to see how many books are in our database.

03:31.040 --> 03:38.300
So instead of race returning this string, I can return our new output.

03:38.960 --> 03:40.520
I need to reference self.

03:41.120 --> 03:42.350
Self output.

03:42.350 --> 03:45.590
Which is because this output is already on this class.

03:45.590 --> 03:48.020
So that means it's available on self.

03:48.020 --> 03:50.900
Self is always referencing to the class.

03:51.050 --> 03:54.610
So we will response this output.

03:54.620 --> 04:03.350
This output is a formatted string and inside string we'll just inject inject length of the books, which

04:03.350 --> 04:07.830
is how many books we have altogether because we select all the books here.

04:07.830 --> 04:15.000
We apply this, assign this to to this variable, and this variable will be displayed here as a length,

04:15.000 --> 04:16.590
which is how many books we have.

04:16.620 --> 04:24.330
So if I will refresh it now, come back to our browser and then we need to go to our previous URL,

04:24.330 --> 04:28.920
which was demo and then another that was our class.

04:28.950 --> 04:32.760
We have to that many books in database.

04:33.390 --> 04:38.520
Uh, actually the, the string makes no sense, so I will change it quickly.

04:38.910 --> 04:41.070
I can do it like that.

04:41.070 --> 04:43.800
So we have two books in database.

04:44.490 --> 04:47.220
I go here, refresh it, we have two books in database.

04:47.220 --> 04:49.380
Indeed, we have two books in database.

04:49.380 --> 04:56.610
So what is happening here is books, objects, all select all books in our system.

04:56.610 --> 05:00.600
So using method all will select all of them.

05:00.930 --> 05:04.440
We have other options and I will show you in a second what we can do.

05:04.440 --> 05:12.000
Actually, here is we can do print, we can do for book in books.

05:15.150 --> 05:19.410
And then we can do something about this, let's say our output.

05:21.710 --> 05:22.670
Will be.

05:23.270 --> 05:24.290
We have.

05:26.790 --> 05:27.600
Book.

05:29.050 --> 05:34.290
And then I can reference any field from that book.

05:34.300 --> 05:37.690
So what I can do is.

05:39.000 --> 05:46.770
I can concatenate, I will need to create some kind of writing a lot of things here at the moment.

05:48.110 --> 05:50.570
I will explain you in a second what I'm doing.

05:54.650 --> 06:00.770
And if I'll do it like that, Let's see what we have so far.

06:02.450 --> 06:08.000
So we have Hobbit book in database and we have Lords of the Ring book in database.

06:08.000 --> 06:09.980
So why we are seeing this?

06:10.010 --> 06:18.170
We are seeing this because first I set up an empty string as an output and then for each book in books,

06:18.170 --> 06:26.690
this is my local variable that is created when I do a loop inside books, which is set of all our books

06:26.690 --> 06:27.590
in the system.

06:27.590 --> 06:35.120
And then for each book I just concatenate a new string to our output, which is at the beginning empty.

06:35.120 --> 06:42.830
So we do, we have and then I can reference this book title inside and the book in database.

06:42.830 --> 06:48.740
That's supposed to be a new line, but I think I need to use another slash.

06:49.160 --> 06:50.060
Or maybe I'm not.

06:50.300 --> 06:51.290
I'm wrong.

06:52.930 --> 06:54.490
Just let me double check.

06:55.780 --> 06:57.930
Now, the double slash doesn't work.

06:57.940 --> 07:00.100
And so what?

07:00.100 --> 07:00.940
I can try.

07:00.940 --> 07:01.690
I can try.

07:01.720 --> 07:04.540
Maybe break.

07:10.050 --> 07:11.700
An email break will work.

07:11.700 --> 07:12.180
So.

07:12.180 --> 07:18.930
So what we are doing here is every time we have a new book in our set, then we'll concatenate to the

07:18.930 --> 07:26.910
string and we will put we have book title book in our database and that's what is printed.

07:26.910 --> 07:32.160
So every time we go through the books, then we display this book title.

07:32.160 --> 07:37.740
And on this book we have all the different fields available.

07:37.740 --> 07:42.810
So cover description is published price, everything is available.

07:42.810 --> 07:44.160
So we have an access to this.

07:44.190 --> 07:49.590
We don't need to print it here, but if we need to do something with any of this, we can do it.

07:49.590 --> 07:57.570
So I will leave it as title because it's a good thing to display and I will just save it once again

07:57.570 --> 08:00.480
and let's come back here and memorize what's here.

08:00.480 --> 08:04.980
So we have Hobbit book in database and we have Lord of the Ring book in database.

08:04.980 --> 08:12.430
Let's add some extra information book, and then we can do with it.

08:13.390 --> 08:24.100
And then I can include ID, So book.id ID by default is added to each model we have, despite the fact

08:24.100 --> 08:25.780
we don't have ID here.

08:25.780 --> 08:27.640
This will be added by Django.

08:27.670 --> 08:33.430
Each object in our database has has its own ID and it is unique.

08:33.430 --> 08:36.640
It started from one and it's incrementing for every new record.

08:36.640 --> 08:38.500
So one, two, three and so on.

08:38.500 --> 08:42.730
So we this is index for our records in databases.

08:43.120 --> 08:44.770
So let's go back to views.

08:44.770 --> 08:52.450
I will save it and if I will come back here and refresh it, you can see The Hobbit book with ID one

08:52.450 --> 08:55.810
and then we have Lord of the Ring book with ID two.

08:55.810 --> 09:03.700
And also I can show you that if I will go to this URL and then slash admin.

09:05.560 --> 09:06.760
And go into the books.

09:06.760 --> 09:11.180
If I will click on this book, you can see one that's actually ID.

09:11.320 --> 09:18.010
This is not represented as a proper string because we haven't picked any field that should be printed

09:18.010 --> 09:18.370
here.

09:18.370 --> 09:25.300
This is just an object, but this one is ID, So if I click here, you can see in the URL we have book,

09:25.420 --> 09:28.270
then ID one and then change.

09:28.270 --> 09:35.410
So this Django admin also recognized this ID, so this is ID one if I will go books.

09:35.500 --> 09:39.190
And here you can see in the URL is ID two.

09:39.190 --> 09:42.400
So this ID we also have then access to this.

09:42.400 --> 09:47.680
So we have two books, hop it with ID one and lots of the ring ID two.

09:48.100 --> 09:54.670
So I'll show you now what other options we have, methods that we can use in the objects.

09:54.670 --> 09:56.650
So all will select all.

09:56.650 --> 09:59.860
And then I will show you a few different methods.

09:59.860 --> 10:06.380
So all we'll select all all records in our database, but we have other options, like, for example,

10:06.380 --> 10:07.100
filter.

10:07.100 --> 10:13.910
What we can do in filter is we can pass some extra parameters to the filter method and it will return

10:13.910 --> 10:18.920
only that object that will meet the criteria that we pass it here.

10:18.920 --> 10:26.990
So for example, what we can do here is we can use any of these fields to do some validation on it.

10:26.990 --> 10:30.140
So what I can do is I can select this published.

10:31.460 --> 10:35.090
For example and I will do is published.

10:37.070 --> 10:37.820
True.

10:38.300 --> 10:43.330
So I will select only this books that are published.

10:43.340 --> 10:51.740
And then if I will go back here and refresh it, you can see lots of the ring Only with ID two is selected

10:51.740 --> 10:54.500
here because lots of the ring is published.

10:54.500 --> 10:55.580
It's marked here.

10:55.580 --> 10:58.790
But if we go back to The Hobbit one, it's not published.

10:58.790 --> 11:05.120
So if I will click publish here, it's published and save it and come back to our view and refresh it.

11:07.430 --> 11:17.270
Actually let's come back here is published, and then we have our Lord of the Ring is published as well.

11:19.170 --> 11:21.270
So we don't see the previous one.

11:28.160 --> 11:35.030
And then I refresh the application and you can see here two books are here because one is published

11:35.270 --> 11:38.600
and another one is also published.

11:38.600 --> 11:44.900
So if I only publish one, I had to refresh the application just because to refresh the database.

11:44.900 --> 11:47.420
But normally you shouldn't do it.

11:47.420 --> 11:48.500
You don't have to do it.

11:48.500 --> 11:53.570
So save one is published, one is not, and then let's try to refresh it.

11:53.570 --> 11:57.800
It might be actually cached here by our browser, this view.

11:57.830 --> 12:02.330
So we might see the old version that is cached.

12:02.330 --> 12:06.920
But if I will go here and then I will refresh the server.

12:08.720 --> 12:11.270
And then I will refresh it here.

12:11.270 --> 12:15.110
You can see now we have a better view.

12:15.140 --> 12:23.330
So basically coming back to here, the filter can filter our records based on some conditions what we

12:23.330 --> 12:24.050
have here.

12:24.050 --> 12:31.940
So what we can also use different things so we can check if a string contains something, we can check

12:31.940 --> 12:35.420
if the value is greater than something or less than something.

12:35.420 --> 12:39.830
And we have a bunch of different options here available in the filter.

12:39.830 --> 12:42.260
So that's a second option.

12:42.260 --> 12:44.360
The third option is Cat.

12:44.720 --> 12:55.970
What Cat will do is get Will brings us just one object all and filter brings us back a set of books.

12:55.970 --> 12:59.900
In this case, Cat will bring only one, so we can't actually do this.

13:00.170 --> 13:05.600
So we need to put a comment there and here because this won't be allowed.

13:05.630 --> 13:10.170
The get always brings one record.

13:10.380 --> 13:19.050
So what I can do here is I can say give me a book of ID two and that will be just one book here.

13:19.590 --> 13:27.660
And then instead of having this output here, what I can do is actually I can copy this one.

13:30.360 --> 13:31.680
And I will put it here.

13:31.680 --> 13:37.050
So we have book title with ID here.

13:37.320 --> 13:38.850
So I will save it now.

13:38.850 --> 13:44.220
And we this time we select just one book with ID two.

13:44.760 --> 13:48.840
I come here and you can see lots of the ring with ID two.

13:48.870 --> 13:56.400
If I will go here and change this ID to one and I will save it and I'm coming back here.

13:56.430 --> 13:57.600
I will refresh it.

13:57.630 --> 14:00.240
It's still the caching problem in the browser.

14:00.240 --> 14:04.080
So what I will do is refresh the the server.

14:05.040 --> 14:06.590
And coming back here.

14:06.600 --> 14:09.840
And then you can see Hobbit with Book one.

14:10.020 --> 14:19.020
So this book objects give us opportunity to access our database and the records in our database.

14:19.020 --> 14:21.180
So book dot objects dot.

14:21.180 --> 14:29.430
And then we have three methods, all selecting all filter returns set of records from our database based

14:29.430 --> 14:36.960
on some conditions and get you also pass some conditions here and that will return only one record.

14:37.800 --> 14:40.280
So that will be it in the next videos.

14:40.290 --> 14:49.200
We will also also show you how the built in Django solution is much better and much more performant

14:49.200 --> 14:51.870
than what we are doing now here manually.
