WEBVTT 00:00.180 --> 00:05.690 Hello everyone and welcome to the mooks API project set up in this new section of the course. 00:05.790 --> 00:12.870 We will create the REST API for a Web site specialized in e-books are API users will be able to get 00:12.870 --> 00:15.580 a list of all these books in apps database. 00:15.750 --> 00:19.280 Add new ones and leave reviews about the available ebooks. 00:19.410 --> 00:24.000 In this first lesson we're going to deal with setting up the project by creating the required orders 00:24.090 --> 00:26.270 and sterilize us so time to code. 00:26.280 --> 00:27.570 Let's get started. 00:28.260 --> 00:34.050 As you can see I've created a new four there called Zero for Django framework Level 2 and there's going 00:34.050 --> 00:39.900 to be within this folder that I'm now going to create a new virtual environment for this specific section 00:40.170 --> 00:42.510 and debug CPA project. 00:42.510 --> 00:47.640 So first of all Python 3 that AM VM. 00:47.970 --> 00:49.610 I'm just going to call the VM. 00:50.460 --> 00:59.010 I'm going to activate the virtual environment and I'm now going to install Django deep install Django 01:00.380 --> 01:01.840 and Django test framework. 01:01.950 --> 01:06.220 People start Django rest framework. 01:07.960 --> 01:17.530 I'm also going to freeze the environment with requirements that text file perfect and I can now create 01:17.620 --> 01:19.570 the new Django project that we're going to use. 01:19.570 --> 01:33.100 So Django had me start project books API so I can now move to the folder and we've got our project we're 01:33.110 --> 01:36.010 going to need an application an app called ebooks. 01:36.350 --> 01:38.970 So I can just as well create this one as well. 01:39.020 --> 01:49.590 Python managed by start up ebooks and clearly I can now just open these wallets and your code is always 01:50.520 --> 01:56.730 so first thing to do I'm going to set up the third so I'm going to select the Python interpreter to 01:56.730 --> 02:02.670 use and thus we can see these bills in your code recognize automatically the virtual environments that 02:02.670 --> 02:04.730 I've used to create the project. 02:04.890 --> 02:07.000 So I'm going to select this one. 02:07.260 --> 02:13.620 I remember you once again that if for any reason visual pseudocode can't manage to find your vehicle 02:13.620 --> 02:21.150 environment from settings workspace set thinks you can just look for VM and add the path that leads 02:21.150 --> 02:24.840 to a file that with the virtual environments that you're using. 02:24.840 --> 02:32.670 So let's now close the settings and let's start to develop our project by actually setting the basic 02:32.670 --> 02:36.010 information that we can add to the settings that pay file. 02:36.480 --> 02:44.610 And clearly I'm referring to adding Django framework to the least of the installed applications and 02:44.700 --> 02:54.440 saving for the e-books app and we can now go ahead and define the models for that app. 02:54.510 --> 02:55.880 So we're going to have two models. 02:55.920 --> 02:59.250 Any book model and a review model. 02:59.400 --> 03:09.150 So let's start off by creating the e-book model so class you book more this model which is going to 03:09.150 --> 03:10.680 have four fields. 03:10.680 --> 03:22.200 So first of all a title model char Chatfield with Max length equals hundred and forty characters. 03:22.200 --> 03:31.310 Then it's going to have a whole third so author equals models and Dot char field with Max length. 03:31.340 --> 03:42.270 We can set 60 characters then it's going to have a description description equals models dot text field 03:42.960 --> 03:45.830 and then it's going to have a date field publication date. 03:45.860 --> 03:52.280 So publication date equals model start date field. 03:52.710 --> 03:56.290 We can now define Dan there's of this thinning representation of our model. 03:56.290 --> 04:05.880 So Def that string which is going to return simply self-taught title let's now create our second model. 04:05.900 --> 04:12.510 The review model so class review models dot model. 04:13.230 --> 04:20.010 So first of all created at model start date time field we've out. 04:20.100 --> 04:23.650 Now ad equals true. 04:23.800 --> 04:29.910 And Dan an updated field updated. 04:29.980 --> 04:39.420 We've daytime field this time out on now through we can now add a review author field so Review author 04:39.450 --> 04:45.610 which is going to be char field we have max length set to eight. 04:45.630 --> 04:49.920 Like a user name but we also want to give our users the chance to be anonymous. 04:49.920 --> 04:58.820 So I'm just going to leave blank equals true and new equals through the review itself is going to be 04:58.820 --> 05:01.470 for the let's say by two parts. 05:01.470 --> 05:06.640 There is going to be a text field which is going to be optional and then a nomadic rating. 05:07.170 --> 05:19.080 So first of all review more than start text field with blank through and normal true and therefore the 05:19.080 --> 05:22.270 numeric rating we are going to need to import to validate. 05:22.350 --> 05:33.210 So from Django dot core dot validators import mean value validator and max value validator. 05:33.510 --> 05:36.340 We can now use them like so. 05:36.480 --> 05:51.710 So rating equals models dot positive integer field and I'm going to pass it validators up with mean 05:52.050 --> 06:02.100 invaluable data set to 1 and max value later set to 5. 06:02.180 --> 06:10.490 We now need to bind the two models together so you book equals models dot 14 key and first of all I'm 06:10.490 --> 06:22.250 going to pass the input class like so and then on do you need models that cascade and as related name 06:22.280 --> 06:25.400 we can just pass reviews. 06:26.630 --> 06:28.870 Let's now defined it understanding my dad. 06:29.450 --> 06:39.710 So Def then the string which is going to return the string version the string representation of rating 06:40.090 --> 06:49.090 or a rather self not rating we can now register and the two models within the ad mean dot by five. 06:49.090 --> 07:00.640 So first of all I'm going to import them from books not models import e-book and review and then write 07:00.640 --> 07:01.330 here below. 07:01.360 --> 07:06.780 Add me outside the register e-book and the same thing. 07:08.710 --> 07:12.020 Applies clearly to the review model. 07:12.330 --> 07:21.150 We can now open up the terminal and give their make migrations command to create all the migrations 07:21.150 --> 07:22.280 for our project. 07:23.700 --> 07:26.420 Create model book create model review. 07:26.610 --> 07:36.990 Now Python manage that by migrate and we can also create a super user so Python managed by create super 07:37.440 --> 07:39.410 user admin 07:43.470 --> 07:47.630 and we can now create the serialized classes for our two models. 07:47.630 --> 07:54.810 So first of all I'm going to create a new folder API and within this folder I'm going to create the 07:54.870 --> 08:08.320 risers that by front and as framework important to realize from the books that the models import e-book 08:08.610 --> 08:10.230 into view. 08:10.780 --> 08:22.780 And now class in the view that I said you realize it's not the model you realize that now class made 08:22.780 --> 08:26.510 up is always model is going to be reviewed. 08:28.070 --> 08:29.000 And fields 08:31.810 --> 08:36.510 specified that we want to have all the fields analyzed. 08:36.620 --> 08:50.610 Let's now define the e-book serialize it plus e-book utilize it like so and this time clearly model 08:50.610 --> 08:52.830 is going to be e-book. 08:53.670 --> 09:00.510 And you can also set in fact fields all but we want to make the relationship between the two models 09:00.570 --> 09:05.000 explicit so reviews equals reviews. 09:05.060 --> 09:16.350 I said with many equals true and read only equals through and we have to see realized that already we 09:16.350 --> 09:20.610 can now start writing day view classes needed for our rest api. 09:20.610 --> 09:25.320 We're going to do so in the next lecture where we're also going to introduce the generic API view class 09:25.560 --> 09:27.940 along with some very powerful mixes. 09:27.960 --> 09:28.470 See you there.