Hello everyone and welcome to the mooks API project set up in this new section of the course.
We will create the REST API for a Web site specialized in e-books are API users will be able to get
a list of all these books in apps database.
Add new ones and leave reviews about the available ebooks.
In this first lesson we're going to deal with setting up the project by creating the required orders
and sterilize us so time to code.
Let's get started.
As you can see I've created a new four there called Zero for Django framework Level 2 and there's going
to be within this folder that I'm now going to create a new virtual environment for this specific section
and debug CPA project.
So first of all Python 3 that AM VM.
I'm just going to call the VM.
I'm going to activate the virtual environment and I'm now going to install Django deep install Django
and Django test framework.
People start Django rest framework.
I'm also going to freeze the environment with requirements that text file perfect and I can now create
the new Django project that we're going to use.
So Django had me start project books API so I can now move to the folder and we've got our project we're
going to need an application an app called ebooks.
So I can just as well create this one as well.
Python managed by start up ebooks and clearly I can now just open these wallets and your code is always
so first thing to do I'm going to set up the third so I'm going to select the Python interpreter to
use and thus we can see these bills in your code recognize automatically the virtual environments that
I've used to create the project.
So I'm going to select this one.
I remember you once again that if for any reason visual pseudocode can't manage to find your vehicle
environment from settings workspace set thinks you can just look for VM and add the path that leads
to a file that with the virtual environments that you're using.
So let's now close the settings and let's start to develop our project by actually setting the basic
information that we can add to the settings that pay file.
And clearly I'm referring to adding Django framework to the least of the installed applications and
saving for the e-books app and we can now go ahead and define the models for that app.
So we're going to have two models.
Any book model and a review model.
So let's start off by creating the e-book model so class you book more this model which is going to
have four fields.
So first of all a title model char Chatfield with Max length equals hundred and forty characters.
Then it's going to have a whole third so author equals models and Dot char field with Max length.
We can set 60 characters then it's going to have a description description equals models dot text field
and then it's going to have a date field publication date.
So publication date equals model start date field.
We can now define Dan there's of this thinning representation of our model.
So Def that string which is going to return simply self-taught title let's now create our second model.
The review model so class review models dot model.
So first of all created at model start date time field we've out.
Now ad equals true.
And Dan an updated field updated.
We've daytime field this time out on now through we can now add a review author field so Review author
which is going to be char field we have max length set to eight.
Like a user name but we also want to give our users the chance to be anonymous.
So I'm just going to leave blank equals true and new equals through the review itself is going to be
for the let's say by two parts.
There is going to be a text field which is going to be optional and then a nomadic rating.
So first of all review more than start text field with blank through and normal true and therefore the
numeric rating we are going to need to import to validate.
So from Django dot core dot validators import mean value validator and max value validator.
We can now use them like so.
So rating equals models dot positive integer field and I'm going to pass it validators up with mean
invaluable data set to 1 and max value later set to 5.
We now need to bind the two models together so you book equals models dot 14 key and first of all I'm
going to pass the input class like so and then on do you need models that cascade and as related name
we can just pass reviews.
Let's now defined it understanding my dad.
So Def then the string which is going to return the string version the string representation of rating
or a rather self not rating we can now register and the two models within the ad mean dot by five.
So first of all I'm going to import them from books not models import e-book and review and then write
here below.
Add me outside the register e-book and the same thing.
Applies clearly to the review model.
We can now open up the terminal and give their make migrations command to create all the migrations
for our project.
Create model book create model review.
Now Python manage that by migrate and we can also create a super user so Python managed by create super
user admin
and we can now create the serialized classes for our two models.
So first of all I'm going to create a new folder API and within this folder I'm going to create the
risers that by front and as framework important to realize from the books that the models import e-book
into view.
And now class in the view that I said you realize it's not the model you realize that now class made
up is always model is going to be reviewed.
And fields
specified that we want to have all the fields analyzed.
Let's now define the e-book serialize it plus e-book utilize it like so and this time clearly model
is going to be e-book.
And you can also set in fact fields all but we want to make the relationship between the two models
explicit so reviews equals reviews.
I said with many equals true and read only equals through and we have to see realized that already we
can now start writing day view classes needed for our rest api.
We're going to do so in the next lecture where we're also going to introduce the generic API view class
along with some very powerful mixes.
See you there.