Hello everyone and welcome to these units competence assessment solution.
Remember that you can download all the code for the project from the downloadable resources of this
section.
Let's start by creating a new Django project.
As you can see I'm currently in the Django is framework level one folder and I have activated the same
virtual environment that I've been using so far in this section.
I'm just going to call the project a job board so a Django admin staff project job board.
And within this project I'm now going to create an app the update we're going to use to actually define
models serialize this and so on.
I'm going to call these app jobs so Python manage that by start up jobs.
I can I will be in Visual Studio code within this folder.
So code dot which means open code within this folder.
And if we are in these words to your code the first thing that I want to do is to choose a python interpreter
for the project.
So control shift B select interpreter.
In this we can see if he's run us through your code detected automatically the same virtual environment
that we've used.
Therefore I can just click on this one.
And of course a Versa your code for that is being created with the same things that the GS file which
points to the current Python path.
So now that the machine is set up the first thing that we want to do is to go ahead and install John
Kerry's framework and the jobs application.
We've been there installed apps a folder.
So here I'm going to add the rest framework like so many jobs I can now go ahead and start writing the
model for our app.
The specifics of this project requires us to write down only one modest job offer.
So right below I'm going to write the class job offer which is going to win everything from models that
model of course.
And now there are a couple of specific fields that we need to add to our model.
So first of all company name which is going to be char field so mothers thought the char field with
Max linked.
We can set up something like 50.
We're going to have company email which is going to be clearly e-mail field then we're going to have
a job title char field.
So job title and maybe this time Max length can be said to 60.
Then we have a job description model dot text field then salary models dot positive integer field that
we need to add Siri which is going to be char field.
I'm just going to copy this code and we can actually set a max length of thirty five.
It's more than enough for chafing for state do we need the time stamp field created at more than a start
date field.
We've outa now ad through so that this field can be managed automatically by January's framework on
our behalf and then a boolean flag available equals false start boolean field with default equals true.
We now have to define the under string as always so self and maybe you can just return the company name.
So return self-taught company name.
Perfect.
We can now open up a terminal in order to create and apply their migrations to our database.
So Python managed that by make migrations create than job offer.
And now Python managed not by migrate.
And yet we see all the migrations gating applied.
We cannot create the super user.
So Python managed not by create super user and mean
OK.
Perfect.
We can now add our model to the I mean that by fire.
So from jobs models import job it.
I'd been outside the register
jump off it.
We can now run our development server which we actually need to set up.
So add configuration and then Python Django I'm going to comment out no threading like so because I'm
using Chrome.
We can now go ahead and actually launch of development server
and we can now create some instances from admin.
So I'm going to go through slash admin me like so so add in new job offer company name Super Corp and
maybe company email jobs at super Corp dot com job title junior developer job description just generic
description salary we can start off by sixty thousand dollars CDM maybe you can set Milan state Italy.
So let's go ahead and save.
Perfect the job offer super corp was added successfully.
So let's go back to these words with your code.
Let's start creating the API code.
So I'm going to create a new folder weaving the jobs book which I'm going to call API and weaving this
fall that I'm going to create three files serialized there's the two by then I'm going to create fuse
that by file and you'll find your address.
By.
So the first piece of code that we want to write for right API is the serialized reckless.
So from rest framework import lasers from jobs not models import and job offer.
And personally I'm going to use the model serialize that class which as we've seen in great detail is
perfect for these kind of tasks.
So class job offer sterilizer supervisors that model civilization.
Yeah I'm going to define class Meta model equals job offer and fields all the fields we can now create
the views.
And in this particular case I'm going to use the API view class I'm going to make a couple of imports
from this framework that generics import get the object of 4 or four then Trump rest framework that
response import response from risk framework that views import API view and from the rest framework
import studies.
And as a matter of fact maybe we could even displace this one on top like so.
So we now need to import the job of our model and the job offer serialize it a class.
So from jobs that models import job offer jobs that API that Syrian realizes import job offer sterilizer
we're now ready to start writing our own API views and as a matter of fact they're going to be very
similar to the one we've written for the news API project.
Therefore I'm just going to copy the code written for the previous project and then I'm going to adapted
for our current use case.
So going to take it remember that there is nothing bad in and not having to reinvent the wheel all the
time especially if you're the one who has written the code and you know perfectly well how it works.
So I'm just going to adapt it so we can use the search or replace functionality.
I'm going to replace with a job offer I'm going to look for article like so and then we can just hit
enter and confirm the replacement and I can maybe just tweak the code a little bit like Jobs feels that
we've active through is actually perfect for our code even though we need to change it we've available
so available through.
We now need to tweak the code a bit more.
So here you can change these two job and you need to return it.
And then for the sake of consistency in the get method one to put method and ended it its methods.
So it seems like everything is just fine.
Let's reload our development server and let's implement the euro.
Start by file.
So first of all from Django not your else import path.
And then out of use from jobs dot API dot views import job offer detail API view.
So you can copy the name from your and also job off at least create API view perfect.
Let's now define the U.S. real partners list.
So first of all path.
Where we can just set it to jobs.
Which is going to call job offer at least create API view with name equals jobs or rather job listings.
We need to call as view so that our class can actually be used.
Then we're going to have a second endpoint clearly.
So thanks so what's going to be jobs.
And an integer primary key is going to call job offer a detailed EPA view dot as view and the name is
going to be job detail.
We now need to connect the you s not by file to date.
You have to start by file that is being automatically created.
Basically the main you are installed by file.
So we need to import include and right here we can't Dan defined path API include jobs not API not you
or else.
So let's restart our development server once again.
Perfect.
Let's now go back to Chrome so we can go and view side.
We got our API and find the source slash API slash job so perfect and it is at least we have all the
available job offers.
Let's add some more.
We now want the F1 course.
So I'm going to delete I.D. and created at City London state UK salary sixty five thousand and you can
just set it generic second company.
So jobs at second company dot org.
Maybe position senior developer.
Let's not post our first job offer via API and perfect to a one created and it are the details of these
specific instance.
So let's go back to the main endpoint.
And here they are both of them.
So let's say that this specific position has been taken.
The one with primary key one so I'm going to update the instance to change its boolean flags.
I'm going to use clearly the same Jason I'm going to delete I.D. and create a dead end.
I'm going to switch available that is now going to be false so let's make it put the requester to perfect
the instance as you can see it's been updated.
Let's go back to at least the had we now only see the second one which is still available.
Let's now test once again the second endpoint.
Let's make a delete request this time.
Are you sure you want to delete delete.
And if it is too old for new content everything succeeded.
Perfect.
So that was it for the solution of this project.
See you in the next lecture which is also going to be the first lecture of the new section Django this
framework.
Level 2.