Hello everyone and welcome back.
In this letter we are going to start developing a user profile based Django project that we are going
to use and customize throughout the whole section.
We will see how to extend the jingles basic user model.
We have a second model that we create allowing us to store further information about our users such
as a user Espanol graphic s.t. and Avatar.
Each user will then be able to write the status messages that will be binding to his profile.
Similarly to what happens in most social networks.
This will also give us the chance to introduce to those of us who do not already know them.
JANGLES signals signals are very powerful because they allow certain senders to notify a set of receivers
that some action is taking place elsewhere in the framework.
In our user profile API project we are going to use the signals to automatically create and bind profiles
instance to a user object as soon as a new one is created.
So let's get started as you can see I've created a new folder called Django as framework Level 3 and
I've also created a new virtual environment that I have already activated and we in this environment.
I've installed Django Django framework and pillow which we are going to need to manage our profiles
about us.
So let's create a profiles API project Django add me.
Start Project profiles API and entities so change directory profile CPI.
Let's create the main app for our project which we can just call profiles.
So start up profiles and this always we can now open these rules to the code within the projects folder
we can now perform all the routine operations so select interpreter and it it is I can use Django this
framework a Level 3 V12 environment and if we have other settings that Jason fine let's now install
both their profiles up and Django framework.
So rest framework
profiles.
And considering that we are now in the settings not by file and we we're going to need to actually use
images in this project we can set media you l and media route right here.
So media you address all media and media root you can just call it uploads.
Perfect.
And actually we can also said that you are real but there is to use in order for our development server
to provide and serve all of the media files.
So this is the code the standard code of course that we need to perform such action and we can now go
ahead and start developing our profiles up.
So let's start by creating the models that we need.
And so specifically we are going to create two models a profile model and a profile start to model.
So far we've been using the User model for all sorts of different things.
First of all and most importantly to authenticate our users.
But we've also connected it to other mothers such as for example the review model in debug CPA project
and so now we are going to explore one of the possible ways that we can extend Django as user model
by connecting it to another model profile which we're also going to create.
This would allow us to store further information about our users such as for example a biography and
how about that.
And we're also going to bind these new profile more than to the profile start to model so that we're
going to use Django as user model only to authenticate users weaving our system enabling this way a
better separation of concerns between the different parts of our project.
This is not the only way that you can extend the Django as user model and as a matter of fact we're
going to exploit it that way to do so later on during the course.
So let's import the User model first from Django dot country dot off dot models import user and now
class profile which is going to extend more not model as any other normal model will do.
So yeah first of all we need to make that connection between the profit model and the User model.
This way user equals model is not one to one field referencing user weave on the ether models dot cascade
and the one to one field the relationship is a perfect fit for these specific tasks because we want
each user to only have access to one profile then we're going to have if you're going to fee model is
not shot field with max length two hundred and forty characters like through we've clearly of course
the capital T then we're going to have a Citi Field SATTERFIELD We have max length of 30 characters
and we're going to have an about that field so models not image field we both know through and blank
through now we know that the user model clearly extends the abstract user model which in itself contains
a couple of really useful fields such as for example User Name First Name Last Name Email is stuff and
so on and so in order for us to properly define a good string representation for our profile more than
we can just use that user name a field that is associated with the use it that would link in their profile
to.
So Def then the string good return self note to use it let's use that name and let's now define their
profile status model so class profile status models not more then.
And the profile status model as for different fields first of all user profile field which we need to
connect using a foreign key field to date profile model we've owned delete models don't cascade is always
there we're going to start those
content equals models Dot charter field with max length equals two hundred and forty characters and
we can add created at equals.
Models Dot.
Date Time field with out.
Now add equals through and and updated at field.
We have our toll now.
Through let us now define the dangerous thinning method.
Yes.
Yeah that self.
We can just as well return the profile that is associated with the profile start.
So.
Third string version of self-taught user profile.
Perfect.
We can now register the two models in the admin dot by fire.
So from profiles to models import profile and profile here.
Add me on site to register profile and safety.
For profile starts now regarding the admin site registration of the different models that is one thing
to keep into consideration and that's is the fact that as you probably know in the admin side Jenga
is going to set the plural version of the name of the model that you registered in and with the international
localization using the English language the plural version of profile status might be.
Profiles start to cease but Django by default only adds an s at the end.
Like for example Apple and apples and so we can't specify right here in the model plus meta.
And here we can set variables.
Name Pluto equals.
We can just set it to statuses.
Perfect.
So it's time to give the commands.
Make migrations and migrate as always.
So it is vital to an environment.
Python managed that by make migrations and Python manage that by migrate and let us also create a super
user.
So Python managed by create super user I mean
perfect.
So let's now close this down and let's run out of developments celebrate instead.
So we need as always to briefly set the configuration for our project.
So the configuration Django no threading commented like so.
So let's start the development of our 3D fine.
Perfect.
Like so and so let's open up google chrome okay.
Here we go.
Slash admin going to log in has had me awesome.
And as you can see we got profiles and statuses and of course we got our art to be an E is let's create
a profile instance for that and use it.
So you see user I can just select add me biography I can add site administrator CTE test land and let's
also choose an avatar.
So as you see in this case I've got a red square that we can select to save.
And this is our first profile instance.
And you also got an avatar that we can show.