Now that our users application is completed in this lesson we can focus on creating the second application
for our project.
The questions application so let's expand the terminal in the python manage that by start up questions.
Let's register diaper right away.
So right yeah we don't after users we begin add questions.
Questions is basically a domain application of our project and instead of it we're going to define all
the code related to both questions and answers.
So let's start off by creating the models in the models stood by file of course.
First of all let's define the question model class question which extends model state model of course
and the fields for this model are the following.
First of all created at model Start Date Time field we have output now and it equals true.
Now we're going to have an update to that field so updated A2 we output.
Now through and to be fair in this project we are never going to show the value of updated ad to our
users via different tend application.
But anyways considering that they will be able to update these instances it is just good practice to
keep track of those changes.
Let's define the other fields.
Of course we are going to have a content field which is the question itself.
So models though the chart field with maximum lengths two hundred and forty characters then it's going
to ever slug field which is the field that we are going to use to retrieve the details of a single question.
And for this like field we can define max length of two hundred and fifty five characters with unique
truth.
Then we are going to have another thought field and the author of Our question is going to be the user
who created it of course.
But considering that we are using a on user model in cases like these it's good practice to reference
the off user model option here in the settings it by file.
Therefore Yeah we can import settings.
So from Django dot confirm import settings.
And so yeah we can define our thought as models start putting key referencing settings not of use it
model with on delete the model is thought cascade and related name equals questions let's now define
done this the angle def and then this thing itself.
And yet it began to return self.
Not content the question itself.
Let's now define the second model for that question supplication deep and square.
Model.
Models don't model.
Let's have these created it and updated ad fields yet as well.
Then we'd been to have the body of the answer this time without the character's limitations.
So model stood.
Text field and of course considering that there can be no answer where we have a question we need to
define a question field so models not for and keep referencing Western
weave on the beta model start cascade and related name answers.
And of course answers that are going to have an author just like these questions.
But this time we can justice keep a related name as we've seen in the first introductory video of the
section users will be able to vote different answers using a like button.
Therefore Yeah we can define that voters field using it many to many relationship reference in the off
user model.
Our custom model with related name equals votes.
Then yeah let's define the boundaries thing mattered for our field as well.
We just going to return self-taught author not user name.
It's time to create and apply the migrations for our new models.
So Python managed that by make migrations and Python administered by migrate let's also register the
models in the ad not by 5.
So first of all from questions that models import answer and question and yeah add me let's site the
register add me.
Or rather elsewhere and of course add me on that site but that I just that question.
There is now one challenge that we need to solve and that's because here we've created the question
model.
We have our slug fields.
And first of all in a context like ours is like field is always created automatically which means that
we need to find a way to automatically create as a lag field a unique lack of field for each question
instance and the field as we said has to be unique of course.
And so this solution that we're going to adopt is the following we we're going to use the signal a three
save signal to create a slug based on the content of a specific question.
So based on the question itself considering that the content field is the question itself then we're
going to use another function to create a shorter string of random characters that we are going to combine
to the previously created slugger based on the content of the question.
And we're going to assign that newly created Stringer as has lined up for that newly created question.
Instance it is actually easier than it might sound.
So let's create the signals that by file and yeah first of all let's import from Django dot the beat
dot models not signals and let's import the print save signal.
Dan from Django Delta dispatch import receiver and then from Django got duties not to them import laggy
fi which we're going to use to create as lag.
Based on the content of the instance and before we continue I remember you did.
As luck is a string which only contains alphanumeric underscores or iPhones and so it's basically the
kind of string that you typically see in you earlier.
Let's now import the question model.
So from questions that models import question and before we continue any further.
As we said we need to create a function to generate a random string of characters and we can create
that function in the core package.
So we're going to create it.
Beauty is not by file India.
Import random and import string.
Let's define alpha numeric chars as string not ASCII lowercase plus string dot digits and below we can
define a string Langton and we can initialize it as C so yeah we can define that function so def generate
random stranger we chars equals alphanumeric chars and linked equals string length.
And here we can return a dot joint random dot choice of characters for each in the range linked we can
now import generate a random string in our signal stood by file so from core dot mutants you bought
it generate a random string.
And we can define a function add log to question sender instance arguments and keyword arguments.
And of course we need to dictate at a function we've received pre save we've sent that equals question
model it's finally time to implement the function.
So if instance and not instance DOT does lag a slug equals slug you fight instance not content then
let's create a random stranger by using the generate a random string function and then yeah we can define
instance dot the logger as slug plus I felt plus random string.
So we're basically manually assigning this lag field to the new e us before it's created so that once
saved the new instance can have its own unique slug to completing the set up for our signal we now need
to go to the OPs dot by file and yet we need to define ready self.
And yet we can import questions not signals in now in debt in it and don't by file default up config
equals questions not perhaps not questions config
like so let's not check that everything works fine so I'm going to open a new terminal Indira Python
manage that by Shell let's import and use it more then so from Django not country not author import
get user model so add a custom user is going to be get user more that is going to be returned by dysfunction
we can now get in the first instance of custom user as custom user not objects not first that which
in this case is Adam Adam you use it let's now import the question more then so from questions that
models import question so that we can create a new question instance.
So question what objects dot create we all thought equals you and content something like first question
does it work enter.
So we have our question is this and if.
Now check this flag over these first question instance we see that every field work perfectly does flag
for these things is being created based on the content of the instance itself.
Plus this random string of characters it's finally time to start developing the rest api part of a question
supplication.