Hello everyone and welcome to Django framework level to come to this assessment solution.
In this lesson we're going to build together one of the possible solutions for the project assigned
the quotes API project and this always I remember you did you can download all the code and the slides
used in each section of the course.
From the first lesson of every section and you will also find the links to the appropriate gate rapidly
from the same lesson.
So let's get started as you can see I'm currently weaving that Django framework level to fall there
where we've been developing so far during this section of the course.
As you can see I've currently activated the same to our environment used in this section.
Therefore I can just give the command Django the main start project to create the new project that we're
going to develop.
In this lesson I can just call it quotes API.
So a lesson and editing is a beautiful project.
And now that we're here we can also create the main up for project which we can just call quotes so
Python manage notes by start up quotes and so with D.
We are now ready to start developing using these words to your code or the editor of your choice.
So I'm going to open this was your code within the project folder like so and we are invisible to your
code.
We can use the control shift beat combination to select and interpret that for this project and we see
that at a moment of work through your code cannot find the development environment that we want to use.
Therefore as always we can just click on File Preferences Settings workspace settings VM and we're just
going to pass the path to that folder what I have defeated what environment that I want to use
copy like so now clicking right here or just using the control shift B combination you see we are able
to select the beautiful environment for Django framework Level 2 and if any problems occur.
Meaning if for any reason visuals your code can't manage to find the virtual environment any way remember
that you can just edit the settings that Jason File and then just manually select the to an environment
that you want to use.
So let's close the settings file and let's install both the quotes up and Django framework.
So rest framework.
And quotes like so and we can now start developing the project as a list starting from the model stood
by file and we can create class quote models not model.
And as you know these projects the specification required is our quote model to have five different
fields.
Quote author quote body context sourced and created at.
And so knowing this we can't start to develop quoth author equals models not char field with max length
fifty is enough then quote body models not text field then context so a context for our quote models
dot char field with Max Planck so we can specify something like two hundred and forty characters like
a tweet but we can also make these feel optional because oftentimes you might not know the exact context
of a specific quote.
Therefore BLANK EQUALS true then super simple models not char field with max length this time under
twenty characters are enough but blank through and then created at the models not Date Time field we
have output.
Now add equals true.
Now considering that quote body uses a text field in the down the string method we can just return it
self.
Dot quote author instead.
Okay cool.
Now that the model is ready we can open up a new terminal.
Like so the environment gets activated and we can create our migrations.
So by phone manage that by make migrations and then Python manage that by migrate and all the migrations
are applied.
Let's also create a super user so create super user admin
as always we are developing for a simple password is fine but to remember that it's vital to use strong
and effective passwords in production.
Now if we wanted to in a real world scenario you might want to actually register in the quote model
within the admin dot py file.
However considering that disease accompanies accessed Assessment Project for a course that is aimed
at teaching you how to use Django as framework we can just skip the admin dot by file and we can create
an API folder instead where we're now going to create all the API related files.
So first of all views that by then he realizes this by and you URLs dot by.
So let's start off by creating the serialized class for our quote model.
So from that as the framework import utilizes and from quotes dot models import quote.
Therefore we can now define class quote utilize it which extends serialize that's not a model C that
I Z India class matter model equals quote We have fields equals and we can select all the fields of
our model.
And now that we've got are seated I said we got started developing our view classes the project specifications
for these quotes API project that required us to use exclusively genetics concrete to view classes and
therefore what we were going to use from the framework import generics then from quotes not models.
Import quote and from quotes dot API dot serialize US import quote serialize it.
Now we know that we will basically need to create two different end points and in point where we are
going to provide create update and delete capabilities for our quote model.
But we also know that we want to allow only Artemis users to actually use these capabilities and then
we know that anyhow I only most users must be able to retrieve basically the available records budget
dated in groups of 30 quotes each.
So as we've seen so far in Django this framework level tool we can create two different classes one
that is going to use the least create API view concrete view class and then the other is going to use
the retrieve update destroy API view class so classic quote least create API view which extends generics
dot least create API view.
So we need to define a query set to use quote dot objects dot all.
And considering that we know that we want to actually generate the results because we know that we need
to provide them in groups of 30 quotes each because of that we can use order by to prevent any progeny
shown errors as you might remember.
So I'm going to order by negative I.D. meaning last records are going to be shown first then you realize
that class equals code serialized there and we're going to think about permissions later on.
For now let's define class and quote detail API view which as we said extends generics dot retrieve
update destroy API view and he's going to basically use the same attributes that we've used up here
except for the order by.
And we are now ready to define the two end points for our quotes API project.
So first of all from Django dot you or else import path and then from quotes dot API dot views import
quote detail EPA view and quote least create API view.
Now your URL but that equals path quotes which is going to call quote least create VBA view dot as you
name it can said something like quote list and then pass quotes does slash integer primary key like
so.
And you're going to call cause detail API if you dot s view with name equals quote in detail.
We now need to include these two and points from within the EU address.
Note by file of the project itself so include and ear.
As always API include quotes dot API dot you else and we are now ready to start the developments that
are very audited to actually check out everything is performing so we need to set the configuration
at configuration Python Django and everything should be fine we have debt default settings except for
no threading which we need to comment out.
So let's start at development server and entities.
He would if he was fine so let's now open up groan let's go to slash API.
We have two points slash quotes ATP 100 OK and editors.
We've got our age to email form and in theory if we wanted to we could start adding instances.
However we know that for this project we want to only add admin use it to be able to create new instances
basically to use these specific point to create new records in our database.
Therefore let's go back to visual studio code and let's go back here.
We can now create permissions
thought by file and yet we are now going to define R is either in use that or read only class to secure.
I didn't create end point.
So from that is the framework to impart that to me shows.
Class is admin use it or read only the dimensions that is admin user.
And here we need to define s permission and we can grab this code define ease I mean and yeah we can
just return request the method in permissions not safe methods or ease I mean we can now import these
classes we've the view is that pie fire.
So actually up here from quotes dot API dot emissions import is admin use it really don't leave and
we can now use it.
We have our classes.
So permission classes equals is I mean user or athlete only.
And the same thing for the detailed end point.
So that update and delete capabilities are granted exclusively to admin users.
So let's restart our development server OK and let's go back right away to Google Chrome let's make
another get request.
You see that the H the email form has disappeared because now we need to logging as admin users.
We could have included Django as frameworks you URLs file in order to be able to logging directly from
the browser API.
However considering our specific development use case it's perfectly fine to just do like so.
So let's create a couple of instances and I've got a couple of quotes here ready to be used quote body
so many books so little time.
Frank Zappa and as source good reads with double O post and now if we wanted to we could clearly also
update you see the instance and let's create a couple more so at Roma without books is like a body without
a soul.
By Marcus tool use the teacher good reads post then maybe a couple more so whatever the mind of a man
can conceive it can achieve by Napoleon Hill and then I attribute my success to this.
I never gave or took any excuses.
So let's create this new quote instance by Florence Nightingale post.
So I actually don't feel that we need to add any more.
Let's go back to these words to your code because we now need to add a final touch to add quotes API
and that's it.
Badger nation of the results.
We can just define everything globally from within the settings dulled by fire.
And so we're going to pass the rest framework dictionary.
We've page size said to 30 as the projected specifications required and default by Jenny show class
clearly and has the framework not Apache Nation not page number Apogee nation.
So let's save and let's restart one less time our development server let's go back to Chrome let's make
another get request.
You see that now we've got to count for next and previous are said to know because we do not have more
than 30 quotes in our database and we got the list of results of the different Jasons of the different
quotes.
Perfect.
Okay.
So that was it for the solution of their competency assessment project for Django framework level to
the next election is going to be the first lecture of Django this framework.
Level 3 what we're going to introduce all the most advanced concepts related to Django rest framework
and developing API is.
We've Django.
And then as you know we're going to start talking about the front end framework of future.
Yes.
In section 6 getting ready to start developing a full fledged single page application we view.
Yes.
And Django this framework in the last section of the course.
So there are a lot of very interesting topics to discuss.
See you in the next lecture.