We now need to create this signals for our project then as we said signals are really awesome because
they allow certain senders to notify you set of receivers that some action has taken place elsewhere
in the framework and in the context of our profile CPA project.
We want to use signals to create a new instance of a profile.
As soon as a new is this of use that is created so that later on when we are going to set up a project
in order to provide some authentication and registration and points we are going to have an underlying
system in charge of making the connection between the two models because let's repeat it one more time.
They use that model is in charge or voluntary authentication related tasks.
And so the first thing that you want to do use Cygnus in a Django project is to create it signals that
by file within the application.
Where do you want these signal communication to take place.
And of course we need to make some imports.
First of all I'm going to import the use that model.
So from Django dot country north of DOT models import user Dan from Django dot D.B. dot models dot signals
import post safe Dan from Django dot dispatch.
Import receiver and finally from profiles dot for this import profile.
So let's discuss what we've just imported.
Clearly we've imported the user and profile models that we've imported the past save signal and a decorator
receiver.
We're going to use the first save a sig now using the user model as the sender to send a signal.
Every time a user instance is saved.
So right after a user instance gets saved a signal is sent and this signal is going to be received from
a function that will go into decode aid using the receiver decorator.
And not only we are going to receive these signal when a user is Nancy's safe.
We're also going to know if they user instance that has sent the signal as just being created or just
modified and so from within the created function we are then going to create a new profile instance
when needed connecting it to the user instance that has sent the signal pretty easy.
So let's implement all of these.
We're going to create the function like a normal function def create profile which is going to accept
a couple of parameters sender is this created and keyword arguments.
We need to decorate create a profile using the receiver signal which is going to accept signal posts
save and which also needs to know the sender.
In this case use it.
Now we can also print just to make it more explicit.
Created the to best created and so are even created again if the instance if they use any instance is
just being created then we're going to create a new profiling instance or profile dot objects to create
with user equals instance.
Again if that specific user instance is just being created we can then create a new profiling star's
profile dot objects dot create with user equals instance.
So again let's explain one more time what's going on here.
We've created this function that is in charge of creating a new profile instances and because of the
receiver and liquidator our function knows that it has to expect a signal sent by user so that in the
context of this specific signal that we are using past save right after a user instance is being saved
the create profile function basically is going to be triggered and because of the created parameter
the function will know if it s to create a new profile instance or not let us know move to the Dan there.
E need file of the profile application because we need to add one line of code default up configuration
config profiles dot ups dot profiles config and to finish their configuration we need to open up the
apps dot by file profiles config and within the class we need to define ready which is going to accept
self.
And we're going to import the signals files so import profiles dot signals now that everything is setup.
Let's restart our development server and let's just leave the terminal open.
Let's go back to Chrome and from here a we can't go back to date site administration interface.
Let's go back to home users and let's modify the ad mean instance.
For example let's say I want to add a first name here Michela going to save and if we now go back to
Venezuela to your code you see right here in the terminal we got created false.
What happened is that when we've saved the admin use it.
Instance right after we've saved it the post saved signal is being sent.
It has been received from the create a profile function which as therefore printed dislike.
But it has not created any new profile instance because clearly created was set to false.
So let's go back one more time to the site administration interface and let us create a new user instead.
So I'm just going to call it random to give a password like so save.
Let's go back to business.
Your code you see.
Now we get created through.
So let's go back once again.
To add been home profiles and entities are new.
Profiling is this that is being created automatically perfect.
Let's now complete the setup for our project by creating this utilize their classes for our models.
So yeah I can just close this one and settings seek had been and instead site it profiles up I'm never
going to create an API folder and weave in this one.
Realizes that by so first of all from rest framework import scene ISIS then from profiles that models
import profile and profile status.
Let's create the Syrian eyes as for our profile first.
So classic profile Syrian eyes that ISIS dot model analyzer.
And so yeah first of all user equals see that ISIS dot string related field.
We've read only equals through then a third equals ISIS dot image field.
We read only through and yet on passing read only equals through because we are going to create a specific
serialized it.
We are going to use only to update the avatar field in our profiles.
And that's because it is so much better off to just create the specific end point to update the avatars
in our profiles.
This is also the same strategy adopted by most social networks that allow you to only update one media
file at a time.
So class Meta model equals profile and we can now set fields equals 0 considering that any way we've
set the way we want to manage user and arbiter and right here.
So let's now create these profile avatars utilize their profile.
How about a nicer she's going to extend more than she realized that just as well.
And here we can't copy.
Plus matter.
But this time we're in fields we're going to define only.
How about that.
Like so.
And let's not define the last satellites that we're going to need plus profile starters.
See I said screen to extend more than seat Eliza and Europe first of all user profile equals serialize
that's not the string related field we've read only through.
And then basically classmate is going to be very similar but of course the model is going to be profile
status.
OK.
So that was it for the set up of our profile CPA project.
See you in the next lecture where we're going to talk about a very important subject authentication
in January's framework.