WEBVTT 00:02.830 --> 00:08.710 Now that our users application is completed in this lesson we can focus on creating the second application 00:08.710 --> 00:09.680 for our project. 00:09.700 --> 00:20.670 The questions application so let's expand the terminal in the python manage that by start up questions. 00:20.950 --> 00:23.600 Let's register diaper right away. 00:24.440 --> 00:30.260 So right yeah we don't after users we begin add questions. 00:30.620 --> 00:36.580 Questions is basically a domain application of our project and instead of it we're going to define all 00:36.580 --> 00:40.150 the code related to both questions and answers. 00:40.180 --> 00:45.540 So let's start off by creating the models in the models stood by file of course. 00:45.730 --> 00:56.160 First of all let's define the question model class question which extends model state model of course 00:56.700 --> 00:59.730 and the fields for this model are the following. 00:59.730 --> 01:10.910 First of all created at model Start Date Time field we have output now and it equals true. 01:10.930 --> 01:17.840 Now we're going to have an update to that field so updated A2 we output. 01:18.130 --> 01:25.180 Now through and to be fair in this project we are never going to show the value of updated ad to our 01:25.180 --> 01:27.820 users via different tend application. 01:27.910 --> 01:34.030 But anyways considering that they will be able to update these instances it is just good practice to 01:34.030 --> 01:35.890 keep track of those changes. 01:35.920 --> 01:37.930 Let's define the other fields. 01:37.990 --> 01:42.370 Of course we are going to have a content field which is the question itself. 01:42.400 --> 01:53.000 So models though the chart field with maximum lengths two hundred and forty characters then it's going 01:53.000 --> 02:00.780 to ever slug field which is the field that we are going to use to retrieve the details of a single question. 02:01.070 --> 02:07.770 And for this like field we can define max length of two hundred and fifty five characters with unique 02:08.440 --> 02:09.560 truth. 02:09.980 --> 02:15.080 Then we are going to have another thought field and the author of Our question is going to be the user 02:15.080 --> 02:16.650 who created it of course. 02:16.820 --> 02:24.390 But considering that we are using a on user model in cases like these it's good practice to reference 02:24.390 --> 02:29.070 the off user model option here in the settings it by file. 02:29.070 --> 02:32.940 Therefore Yeah we can import settings. 02:32.940 --> 02:37.690 So from Django dot confirm import settings. 02:37.770 --> 02:48.060 And so yeah we can define our thought as models start putting key referencing settings not of use it 02:48.880 --> 03:00.930 model with on delete the model is thought cascade and related name equals questions let's now define 03:01.850 --> 03:06.100 done this the angle def and then this thing itself. 03:06.450 --> 03:08.180 And yet it began to return self. 03:08.190 --> 03:11.250 Not content the question itself. 03:11.250 --> 03:16.610 Let's now define the second model for that question supplication deep and square. 03:16.950 --> 03:17.580 Model. 03:17.580 --> 03:19.960 Models don't model. 03:20.520 --> 03:24.940 Let's have these created it and updated ad fields yet as well. 03:25.590 --> 03:31.310 Then we'd been to have the body of the answer this time without the character's limitations. 03:31.320 --> 03:32.840 So model stood. 03:32.970 --> 03:39.360 Text field and of course considering that there can be no answer where we have a question we need to 03:39.360 --> 03:48.040 define a question field so models not for and keep referencing Western 03:51.100 --> 04:01.670 weave on the beta model start cascade and related name answers. 04:01.830 --> 04:07.680 And of course answers that are going to have an author just like these questions. 04:08.070 --> 04:15.780 But this time we can justice keep a related name as we've seen in the first introductory video of the 04:15.780 --> 04:20.980 section users will be able to vote different answers using a like button. 04:21.030 --> 04:29.010 Therefore Yeah we can define that voters field using it many to many relationship reference in the off 04:29.130 --> 04:30.000 user model. 04:30.030 --> 04:39.960 Our custom model with related name equals votes. 04:40.260 --> 04:45.480 Then yeah let's define the boundaries thing mattered for our field as well. 04:47.110 --> 04:52.020 We just going to return self-taught author not user name. 04:52.240 --> 04:55.530 It's time to create and apply the migrations for our new models. 04:55.540 --> 05:06.730 So Python managed that by make migrations and Python administered by migrate let's also register the 05:06.740 --> 05:09.710 models in the ad not by 5. 05:09.790 --> 05:20.740 So first of all from questions that models import answer and question and yeah add me let's site the 05:20.840 --> 05:23.800 register add me. 05:23.920 --> 05:32.700 Or rather elsewhere and of course add me on that site but that I just that question. 05:32.880 --> 05:39.730 There is now one challenge that we need to solve and that's because here we've created the question 05:39.730 --> 05:40.390 model. 05:40.470 --> 05:42.300 We have our slug fields. 05:42.460 --> 05:49.180 And first of all in a context like ours is like field is always created automatically which means that 05:49.210 --> 05:55.950 we need to find a way to automatically create as a lag field a unique lack of field for each question 05:56.050 --> 06:00.880 instance and the field as we said has to be unique of course. 06:00.880 --> 06:05.740 And so this solution that we're going to adopt is the following we we're going to use the signal a three 06:05.800 --> 06:11.770 save signal to create a slug based on the content of a specific question. 06:11.830 --> 06:17.970 So based on the question itself considering that the content field is the question itself then we're 06:17.980 --> 06:24.100 going to use another function to create a shorter string of random characters that we are going to combine 06:24.100 --> 06:28.730 to the previously created slugger based on the content of the question. 06:28.960 --> 06:36.230 And we're going to assign that newly created Stringer as has lined up for that newly created question. 06:36.250 --> 06:40.120 Instance it is actually easier than it might sound. 06:40.120 --> 06:48.960 So let's create the signals that by file and yeah first of all let's import from Django dot the beat 06:49.030 --> 06:55.480 dot models not signals and let's import the print save signal. 06:56.300 --> 07:08.170 Dan from Django Delta dispatch import receiver and then from Django got duties not to them import laggy 07:08.190 --> 07:11.190 fi which we're going to use to create as lag. 07:11.350 --> 07:15.860 Based on the content of the instance and before we continue I remember you did. 07:15.870 --> 07:22.600 As luck is a string which only contains alphanumeric underscores or iPhones and so it's basically the 07:22.600 --> 07:25.610 kind of string that you typically see in you earlier. 07:25.720 --> 07:27.550 Let's now import the question model. 07:27.550 --> 07:35.290 So from questions that models import question and before we continue any further. 07:35.320 --> 07:41.350 As we said we need to create a function to generate a random string of characters and we can create 07:41.380 --> 07:43.570 that function in the core package. 07:43.570 --> 07:45.330 So we're going to create it. 07:45.410 --> 07:49.160 Beauty is not by file India. 07:49.720 --> 07:54.770 Import random and import string. 07:55.720 --> 08:11.410 Let's define alpha numeric chars as string not ASCII lowercase plus string dot digits and below we can 08:11.410 --> 08:20.950 define a string Langton and we can initialize it as C so yeah we can define that function so def generate 08:22.210 --> 08:35.200 random stranger we chars equals alphanumeric chars and linked equals string length. 08:35.710 --> 08:49.210 And here we can return a dot joint random dot choice of characters for each in the range linked we can 08:49.210 --> 08:57.120 now import generate a random string in our signal stood by file so from core dot mutants you bought 08:57.140 --> 08:59.500 it generate a random string. 08:59.860 --> 09:12.850 And we can define a function add log to question sender instance arguments and keyword arguments. 09:13.030 --> 09:23.440 And of course we need to dictate at a function we've received pre save we've sent that equals question 09:23.440 --> 09:27.060 model it's finally time to implement the function. 09:27.130 --> 09:41.080 So if instance and not instance DOT does lag a slug equals slug you fight instance not content then 09:41.110 --> 09:50.680 let's create a random stranger by using the generate a random string function and then yeah we can define 09:50.680 --> 09:59.150 instance dot the logger as slug plus I felt plus random string. 09:59.560 --> 10:06.720 So we're basically manually assigning this lag field to the new e us before it's created so that once 10:06.740 --> 10:13.930 saved the new instance can have its own unique slug to completing the set up for our signal we now need 10:13.930 --> 10:21.590 to go to the OPs dot by file and yet we need to define ready self. 10:22.990 --> 10:36.400 And yet we can import questions not signals in now in debt in it and don't by file default up config 10:36.550 --> 10:43.940 equals questions not perhaps not questions config 10:46.800 --> 10:55.750 like so let's not check that everything works fine so I'm going to open a new terminal Indira Python 10:56.400 --> 11:06.460 manage that by Shell let's import and use it more then so from Django not country not author import 11:06.670 --> 11:18.430 get user model so add a custom user is going to be get user more that is going to be returned by dysfunction 11:18.940 --> 11:27.550 we can now get in the first instance of custom user as custom user not objects not first that which 11:27.550 --> 11:34.570 in this case is Adam Adam you use it let's now import the question more then so from questions that 11:35.110 --> 11:41.210 models import question so that we can create a new question instance. 11:41.410 --> 11:54.430 So question what objects dot create we all thought equals you and content something like first question 11:55.120 --> 11:59.380 does it work enter. 12:00.050 --> 12:02.930 So we have our question is this and if. 12:02.930 --> 12:10.610 Now check this flag over these first question instance we see that every field work perfectly does flag 12:10.700 --> 12:16.190 for these things is being created based on the content of the instance itself. 12:16.190 --> 12:23.870 Plus this random string of characters it's finally time to start developing the rest api part of a question 12:23.910 --> 12:24.620 supplication.