In this video.
We'll come back to our admin page and I will show you how we can actually use built in admin and extend
it for our needs.
So I will open admin and you can see here we have our model registered here and I will show you another
way how we can actually register our models that will give us much more control over what we would like
to see and how we would like to see it.
So first, let's come back to our admin page here.
So instead of demo first I will go admin.
And I can see we are here.
So we'll go to the books and this is what we see.
So first thing, what I would like to do is instead of tag book object here, this is not really human
readable.
We can handle this.
And the way we handle this is going to our models.
We have our book here, so that's a list of fields we have available.
But also we can specify how our class will be converted to string.
At the moment, Django doesn't really know how to do our book, how to convert it to string to read
it.
So it's a book object for him and that's the best he can do.
But we can actually use our own method to do that.
We can do def and then underscore, underscore, str like string, underscore, underscore and you can
see rest of it is we can actually click enter and it will be like that.
So definition and then underscore, underscore str, underscore, underscore and then self to refer
to our class and in here we can return, return, whatever we like.
In our case, what will be the good data to display for a book?
Obviously title.
So what I can do is self.
The title and we are telling that every time we would like to see our book displayed as a string, let's
use title for it.
If I will save it now and I come back here and refresh it, you can see instead of that object now we
can see title.
So that's one thing that we've changed.
So let's go to the one of those and you can see we have that kind of fields here.
If we come back to our admin page, we can actually change it and control what we see.
This is very generic admin site register book, but I will show you another way we can actually deal
with that and register our book with some more greater control.
So what we can do is we will use decorator, admin dot register and then we can register our book here
like that.
And then what we need to do is we need to create a class.
So we will do book admin, we can name it whatever we like, but book admin, it's a good convention.
The first part is our model and then admin here.
And then inside we will pass admin model, admin like that.
And then we basically what's what this is doing is we still register our book and then we will use it
in the class and then we have some extra options.
I will show you a few.
So first we can specify the fields.
So let's say I would like to show title as a field available if I will save it now and come back here.
And that was what did.
This is what it was before.
If we refresh it now, you can see only title is displayed.
The reason for this is because we decided that title is the only option we should display.
So if I will add another one, like for example, description here, save it, we'll come back here,
refresh it.
You can see title and description is displayed.
So if you don't need some of the fields to be displayed in the UI, then you can hide it or you can
decide which one you would like to include in your in your UI.
Another thing we can do is list display and then let's say we can display title and description.
And for now, I will remove these fields and we'll revert it back to how it was.
But this time we'll use a list display.
So we can keep the boat.
You don't need to have one or another, but I will remove it just for easier.
So I will refresh it and you can see all of it.
It's back because the fields, we don't have anything in the fields.
But if I will come back to the books, you can see here we have a table, the table with a title and
description.
So now the title is here and description is here.
If you would like to have something else displayed in this view, you can specify this here.
So let's say price is something you would like to have in that view.
Come back here, refresh.
I haven't saved, I think, this file.
Refresh.
And you can see a lot of this is a title and this is a price.
So what else is available?
We can do list filter, list, filter, for example, and then we can decide on which one we will filter.
Let's say we can filter by published.
So I will put it here.
I will save that and come back here and then I will refresh it.
You can see new filter has been added to our admin page and we have some filters here.
This depends on the type of the field that we are filtering.
We are filtering by date.
So this is common to like this month past seven days, seven days today and so on and so on.
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
you like and you will have that handy pane for filtering.
Another option will be search fields so we can have a search in the same page.
And let's say we would like to search by title.
I will save it here and come back to our admin, refresh it and you can see on the top of the page we
have Search Hobbit and then I will click search and you can see only Hobbit is displayed here because
I searched for this if I.
I specify here.
More fields.
Description.
For example, the search will go for title and description and the return result will be matching that
record.
So title and description.
We don't have much in the description, so this is not going to work.
But in in fact this two totals, this is without search and then I can filter or find my records in
database using this.
As you can see, this page start to be very user friendly and that might be your entire application.
What I mean by application some sometimes you you need to have something to store records in the database.
You need to have some kind of view user interface and login and you have all that built in Django.
So if you need that simple things, you can do user authentication and then you can provide them an
optimize views to manage the the resources on the database.
As you can see, you can use filters, you can decide what you include in this list.
You can have a custom search and also you can decide what they will be able to edit here.
So that's pretty much it.
That's how Django administration works.
There is much more than I will show you, but I've showed you the basic one, the most important part.
But you can also go to a official Django documentation to see what the what the other options are.
But as you can see, this is very powerful view and give it to us for free.