WEBVTT

00:01.240 --> 00:02.410
In this video.

00:02.410 --> 00:10.690
We'll come back to our admin page and I will show you how we can actually use built in admin and extend

00:10.690 --> 00:11.870
it for our needs.

00:11.880 --> 00:20.560
So I will open admin and you can see here we have our model registered here and I will show you another

00:20.560 --> 00:27.550
way how we can actually register our models that will give us much more control over what we would like

00:27.550 --> 00:29.950
to see and how we would like to see it.

00:29.950 --> 00:33.370
So first, let's come back to our admin page here.

00:33.370 --> 00:35.950
So instead of demo first I will go admin.

00:37.590 --> 00:38.910
And I can see we are here.

00:38.910 --> 00:42.150
So we'll go to the books and this is what we see.

00:42.890 --> 00:50.960
So first thing, what I would like to do is instead of tag book object here, this is not really human

00:50.960 --> 00:51.710
readable.

00:51.710 --> 00:53.570
We can handle this.

00:53.600 --> 00:58.460
And the way we handle this is going to our models.

00:58.790 --> 01:04.330
We have our book here, so that's a list of fields we have available.

01:04.340 --> 01:11.840
But also we can specify how our class will be converted to string.

01:11.870 --> 01:18.440
At the moment, Django doesn't really know how to do our book, how to convert it to string to read

01:18.440 --> 01:18.560
it.

01:18.560 --> 01:22.490
So it's a book object for him and that's the best he can do.

01:22.490 --> 01:25.940
But we can actually use our own method to do that.

01:25.970 --> 01:33.380
We can do def and then underscore, underscore, str like string, underscore, underscore and you can

01:33.380 --> 01:37.970
see rest of it is we can actually click enter and it will be like that.

01:37.970 --> 01:45.750
So definition and then underscore, underscore str, underscore, underscore and then self to refer

01:45.750 --> 01:52.410
to our class and in here we can return, return, whatever we like.

01:52.440 --> 01:56.970
In our case, what will be the good data to display for a book?

01:57.000 --> 01:58.170
Obviously title.

01:58.170 --> 01:59.820
So what I can do is self.

02:01.310 --> 02:10.970
The title and we are telling that every time we would like to see our book displayed as a string, let's

02:10.970 --> 02:12.950
use title for it.

02:12.980 --> 02:20.750
If I will save it now and I come back here and refresh it, you can see instead of that object now we

02:20.750 --> 02:21.770
can see title.

02:21.770 --> 02:24.500
So that's one thing that we've changed.

02:24.530 --> 02:30.980
So let's go to the one of those and you can see we have that kind of fields here.

02:30.980 --> 02:38.210
If we come back to our admin page, we can actually change it and control what we see.

02:38.420 --> 02:45.590
This is very generic admin site register book, but I will show you another way we can actually deal

02:45.590 --> 02:50.900
with that and register our book with some more greater control.

02:50.930 --> 03:00.870
So what we can do is we will use decorator, admin dot register and then we can register our book here

03:00.870 --> 03:01.830
like that.

03:01.830 --> 03:05.490
And then what we need to do is we need to create a class.

03:05.490 --> 03:13.140
So we will do book admin, we can name it whatever we like, but book admin, it's a good convention.

03:13.140 --> 03:17.430
The first part is our model and then admin here.

03:18.240 --> 03:25.830
And then inside we will pass admin model, admin like that.

03:25.830 --> 03:34.680
And then we basically what's what this is doing is we still register our book and then we will use it

03:34.680 --> 03:38.550
in the class and then we have some extra options.

03:38.550 --> 03:39.710
I will show you a few.

03:39.720 --> 03:42.330
So first we can specify the fields.

03:42.330 --> 03:52.230
So let's say I would like to show title as a field available if I will save it now and come back here.

03:52.230 --> 03:54.650
And that was what did.

03:54.660 --> 03:56.370
This is what it was before.

03:56.400 --> 04:01.080
If we refresh it now, you can see only title is displayed.

04:01.080 --> 04:07.860
The reason for this is because we decided that title is the only option we should display.

04:07.860 --> 04:17.460
So if I will add another one, like for example, description here, save it, we'll come back here,

04:17.460 --> 04:18.240
refresh it.

04:18.270 --> 04:21.540
You can see title and description is displayed.

04:21.540 --> 04:30.660
So if you don't need some of the fields to be displayed in the UI, then you can hide it or you can

04:30.660 --> 04:36.810
decide which one you would like to include in your in your UI.

04:37.350 --> 04:46.740
Another thing we can do is list display and then let's say we can display title and description.

04:47.730 --> 04:53.280
And for now, I will remove these fields and we'll revert it back to how it was.

04:53.280 --> 04:55.680
But this time we'll use a list display.

04:56.490 --> 04:57.750
So we can keep the boat.

04:58.170 --> 05:03.720
You don't need to have one or another, but I will remove it just for easier.

05:03.720 --> 05:06.720
So I will refresh it and you can see all of it.

05:06.720 --> 05:09.810
It's back because the fields, we don't have anything in the fields.

05:09.810 --> 05:16.980
But if I will come back to the books, you can see here we have a table, the table with a title and

05:16.980 --> 05:17.820
description.

05:17.820 --> 05:20.880
So now the title is here and description is here.

05:20.880 --> 05:28.470
If you would like to have something else displayed in this view, you can specify this here.

05:28.470 --> 05:32.730
So let's say price is something you would like to have in that view.

05:33.660 --> 05:36.000
Come back here, refresh.

05:38.530 --> 05:40.960
I haven't saved, I think, this file.

05:48.500 --> 05:49.280
Refresh.

05:49.280 --> 05:53.690
And you can see a lot of this is a title and this is a price.

05:55.520 --> 05:57.890
So what else is available?

05:58.580 --> 06:10.100
We can do list filter, list, filter, for example, and then we can decide on which one we will filter.

06:10.100 --> 06:12.740
Let's say we can filter by published.

06:13.460 --> 06:15.410
So I will put it here.

06:15.830 --> 06:19.640
I will save that and come back here and then I will refresh it.

06:19.670 --> 06:27.020
You can see new filter has been added to our admin page and we have some filters here.

06:27.020 --> 06:32.150
This depends on the type of the field that we are filtering.

06:32.150 --> 06:34.280
We are filtering by date.

06:34.280 --> 06:40.910
So this is common to like this month past seven days, seven days today and so on and so on.

06:41.330 --> 06:48.960
So this is the the filter and we you can pass more than one filter to it so we can pass it as many as

06:48.960 --> 06:53.190
you like and you will have that handy pane for filtering.

06:54.060 --> 07:05.490
Another option will be search fields so we can have a search in the same page.

07:05.490 --> 07:08.070
And let's say we would like to search by title.

07:08.670 --> 07:15.030
I will save it here and come back to our admin, refresh it and you can see on the top of the page we

07:15.030 --> 07:23.850
have Search Hobbit and then I will click search and you can see only Hobbit is displayed here because

07:23.850 --> 07:26.270
I searched for this if I.

07:27.770 --> 07:29.210
I specify here.

07:29.210 --> 07:31.730
More fields.

07:31.760 --> 07:33.440
Description.

07:33.470 --> 07:42.590
For example, the search will go for title and description and the return result will be matching that

07:42.590 --> 07:43.040
record.

07:43.040 --> 07:45.020
So title and description.

07:45.020 --> 07:48.410
We don't have much in the description, so this is not going to work.

07:48.410 --> 07:58.010
But in in fact this two totals, this is without search and then I can filter or find my records in

07:58.010 --> 07:59.420
database using this.

07:59.450 --> 08:06.920
As you can see, this page start to be very user friendly and that might be your entire application.

08:07.190 --> 08:14.720
What I mean by application some sometimes you you need to have something to store records in the database.

08:14.720 --> 08:20.870
You need to have some kind of view user interface and login and you have all that built in Django.

08:20.900 --> 08:28.950
So if you need that simple things, you can do user authentication and then you can provide them an

08:28.950 --> 08:35.250
optimize views to manage the the resources on the database.

08:35.250 --> 08:39.660
As you can see, you can use filters, you can decide what you include in this list.

08:39.660 --> 08:45.210
You can have a custom search and also you can decide what they will be able to edit here.

08:45.210 --> 08:46.800
So that's pretty much it.

08:46.800 --> 08:50.070
That's how Django administration works.

08:50.070 --> 08:56.220
There is much more than I will show you, but I've showed you the basic one, the most important part.

08:56.220 --> 09:02.790
But you can also go to a official Django documentation to see what the what the other options are.

09:02.820 --> 09:08.430
But as you can see, this is very powerful view and give it to us for free.
