Okay.
So far we display a dummy text here in our web application, but it has nothing to do with what we have
in our Django, which is inside our models.
We have something that we can actually use and that's coming from the database.
So we seen that book already in the admin page.
And let's go to the admin and let's do something there.
So if I go admin.
I have just one book here.
I can add another one.
Lord of the Ring or whatever some description goes here and so on.
The price, I can have it for one and publish I can do it today is publish.
It is.
And then I will just leave it there.
I'll make it a bigger screen and I will save it.
So we have two books.
One book is Hobbit and another book is Lord of the Ring.
So that's what we have in our database now.
So let's come back to our application here and goes to the views.
And what we can do actually is we can use whatever is in our database and we can do something about
it anywhere in our application.
It doesn't have to be views, but views is a very good candidate to use it because we can actually grab
something from our database and push it to our view, which is displayed on the screen for the user
and what we can do.
First, we need to import.
So from dot models, dot always means from the current folder, we are inside the views.
As you can see here, model synthesis is in the same folder, so we'll import from the models and then
we'll import book.
So the book is available for us now.
And then the book was just a this class here that has all of this included.
But what we can do here is we can actually reference some some books here.
So I will do books.
And then it will be equal to something.
And in Django, we can reference the model like Doc, which is name of the class book and then objects.
And then we can do, for example, all.
So what does it mean?
We're taking books, our model, we reference all objects of that model and then we want to select all
of them and books now.
Now they represent the set of our books.
So what we can do here is we can actually create a new string.
So output, I can name it whatever I like, but I will do a format here, which is just a formatting
the string and we'll say we have.
And then that many books in database.
So what's happening here is actually I format the string.
And inside here what I can do is I can inject length, which is length of our array of books.
And we can we will be able to see how many books are in our database.
So instead of race returning this string, I can return our new output.
I need to reference self.
Self output.
Which is because this output is already on this class.
So that means it's available on self.
Self is always referencing to the class.
So we will response this output.
This output is a formatted string and inside string we'll just inject inject length of the books, which
is how many books we have altogether because we select all the books here.
We apply this, assign this to to this variable, and this variable will be displayed here as a length,
which is how many books we have.
So if I will refresh it now, come back to our browser and then we need to go to our previous URL,
which was demo and then another that was our class.
We have to that many books in database.
Uh, actually the, the string makes no sense, so I will change it quickly.
I can do it like that.
So we have two books in database.
I go here, refresh it, we have two books in database.
Indeed, we have two books in database.
So what is happening here is books, objects, all select all books in our system.
So using method all will select all of them.
We have other options and I will show you in a second what we can do.
Actually, here is we can do print, we can do for book in books.
And then we can do something about this, let's say our output.
Will be.
We have.
Book.
And then I can reference any field from that book.
So what I can do is.
I can concatenate, I will need to create some kind of writing a lot of things here at the moment.
I will explain you in a second what I'm doing.
And if I'll do it like that, Let's see what we have so far.
So we have Hobbit book in database and we have Lords of the Ring book in database.
So why we are seeing this?
We are seeing this because first I set up an empty string as an output and then for each book in books,
this is my local variable that is created when I do a loop inside books, which is set of all our books
in the system.
And then for each book I just concatenate a new string to our output, which is at the beginning empty.
So we do, we have and then I can reference this book title inside and the book in database.
That's supposed to be a new line, but I think I need to use another slash.
Or maybe I'm not.
I'm wrong.
Just let me double check.
Now, the double slash doesn't work.
And so what?
I can try.
Maybe break.
An email break will work.
So.
So what we are doing here is every time we have a new book in our set, then we'll concatenate to the
string and we will put we have book title book in our database and that's what is printed.
So every time we go through the books, then we display this book title.
And on this book we have all the different fields available.
So cover description is published price, everything is available.
So we have an access to this.
We don't need to print it here, but if we need to do something with any of this, we can do it.
So I will leave it as title because it's a good thing to display and I will just save it once again
and let's come back here and memorize what's here.
So we have Hobbit book in database and we have Lord of the Ring book in database.
Let's add some extra information book, and then we can do with it.
And then I can include ID, So book.id ID by default is added to each model we have, despite the fact
we don't have ID here.
This will be added by Django.
Each object in our database has has its own ID and it is unique.
It started from one and it's incrementing for every new record.
So one, two, three and so on.
So we this is index for our records in databases.
So let's go back to views.
I will save it and if I will come back here and refresh it, you can see The Hobbit book with ID one
and then we have Lord of the Ring book with ID two.
And also I can show you that if I will go to this URL and then slash admin.
And go into the books.
If I will click on this book, you can see one that's actually ID.
This is not represented as a proper string because we haven't picked any field that should be printed
here.
This is just an object, but this one is ID, So if I click here, you can see in the URL we have book,
then ID one and then change.
So this Django admin also recognized this ID, so this is ID one if I will go books.
And here you can see in the URL is ID two.
So this ID we also have then access to this.
So we have two books, hop it with ID one and lots of the ring ID two.
So I'll show you now what other options we have, methods that we can use in the objects.
So all will select all.
And then I will show you a few different methods.
So all we'll select all all records in our database, but we have other options, like, for example,
filter.
What we can do in filter is we can pass some extra parameters to the filter method and it will return
only that object that will meet the criteria that we pass it here.
So for example, what we can do here is we can use any of these fields to do some validation on it.
So what I can do is I can select this published.
For example and I will do is published.
True.
So I will select only this books that are published.
And then if I will go back here and refresh it, you can see lots of the ring Only with ID two is selected
here because lots of the ring is published.
It's marked here.
But if we go back to The Hobbit one, it's not published.
So if I will click publish here, it's published and save it and come back to our view and refresh it.
Actually let's come back here is published, and then we have our Lord of the Ring is published as well.
So we don't see the previous one.
And then I refresh the application and you can see here two books are here because one is published
and another one is also published.
So if I only publish one, I had to refresh the application just because to refresh the database.
But normally you shouldn't do it.
You don't have to do it.
So save one is published, one is not, and then let's try to refresh it.
It might be actually cached here by our browser, this view.
So we might see the old version that is cached.
But if I will go here and then I will refresh the server.
And then I will refresh it here.
You can see now we have a better view.
So basically coming back to here, the filter can filter our records based on some conditions what we
have here.
So what we can also use different things so we can check if a string contains something, we can check
if the value is greater than something or less than something.
And we have a bunch of different options here available in the filter.
So that's a second option.
The third option is Cat.
What Cat will do is get Will brings us just one object all and filter brings us back a set of books.
In this case, Cat will bring only one, so we can't actually do this.
So we need to put a comment there and here because this won't be allowed.
The get always brings one record.
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.
And then instead of having this output here, what I can do is actually I can copy this one.
And I will put it here.
So we have book title with ID here.
So I will save it now.
And we this time we select just one book with ID two.
I come here and you can see lots of the ring with ID two.
If I will go here and change this ID to one and I will save it and I'm coming back here.
I will refresh it.
It's still the caching problem in the browser.
So what I will do is refresh the the server.
And coming back here.
And then you can see Hobbit with Book one.
So this book objects give us opportunity to access our database and the records in our database.
So book dot objects dot.
And then we have three methods, all selecting all filter returns set of records from our database based
on some conditions and get you also pass some conditions here and that will return only one record.
So that will be it in the next videos.
We will also also show you how the built in Django solution is much better and much more performant
than what we are doing now here manually.