Introduction to Django Signals | Django (3.0) Crash Course Tutorials (pt 18)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody so in this video I'm gonna introduce you to the concept of Django signals and show you how signals can really help simplify a lot of our code whenever we have multiple bits of code interested in the same event so signals are basically comprised of ascenders and receivers that send out some information to a receiver or whenever something has occurred so we're gonna start with model signals and the example I like to use is a relationship between my default Django user model and how I extend it with a profile so every single user is going to have a profile in my system so every single time a user is created or updated I want a sender to be sent out and either create or update the profile also so it's just basically going to be an event listener that's going to be added to that save method and send out some information so these two will be made into my receivers and will again either create or update a profile based on whatever the signal sent out to these receivers and we'll need to add a little bit of code to actually make these into receivers so they respond but we'll handle that in a second and also really quick know if you're part of the Django crash-course series I'm making this video as a standalone project so if you look at our code it's not the same project as that CRM we're making but I will make one more follow-up video implementing this into that registration form so for now we're gonna write all of our logic within models py but the Django documentation does recommend that we write all of our signals within its own file so later on in this video we're actually in and create a file called signals up py and go ahead and just copy and paste all of this in there but because that does require a little bit more configuration we're actually gonna write it right in here to simplify things and then we'll just move it there later so let's go ahead and add some parameters and make this receiver and then actually connect it to a signal so from the Django documentation we're gonna use this post underscore save method here so let's go ahead and run this import and then in the post save we're gonna add these parameters to that function so the sender which is the model that's supposed to send the signal the instance of that model so the actual item or the object that's sending it and then this creative attribute here which basically if this is the first creation of that item or if this is just an update so let's go ahead and do that so within here let's run an import so this will be from Django dot DB models dot signals import and we're gonna use the pre save so this is going to be a model signal and it's gonna be pre underscore save and in here what we can do is go ahead and pass in those parameters so the sender the model that's sending the signal the instance of it and the created instance was checks if this is a new one in we're just gonna go ahead and pass in star star quark so any extra keyword arguments will pass in that way and in here if i uncomment this what we're doing here is whenever that signal calls this function this receiver we're gonna check if this is the first instance if this is we'll go ahead and create a new profile so we'll do profile objects create user in it's going to be this instance so it'll go ahead and set it in we have ourselves a new profile but whenever we update an item so we'll go ahead and uncomment this we'll pass in these parameters and I actually decided to get rid of some stuff here so let's go ahead and actually copy that and I'll paste that in here and let me just remove this so sorry if you actually wrote that out already so basically what we're doing here is calling the same thing but we're checking created again and if created as false which means that this user already exists let's go ahead and update that user profile so instance dot profile save and in here you can write whatever logic you want maybe update certain parameters but I'm gonna keep it simple and just called a save method on that profile so in order to connect this now all we have to do and there's actually a few ways to do this all we're gonna do here is go ahead actually just realize we changed where we messed this up this needs to be post underscore save so we're gonna do post underscore save dot connect and we're gonna pass in two parameters here so this is how we connect a receiver to a sender so the receiver is going to be this create profile function here and the sender is going to be the model that's gonna trigger it so this user model every single time the save method is called we're gonna go ahead and trigger this receive method here or this create profile function after the save is complete so post save after save go ahead and trigger that function and listen to this model here so we can actually go ahead and copy and paste out there and change it for update profile and every single time the save method is called both of these functions will be triggered but they will respond differently so I do have one user in the database that does not have a profile so to not give us an error go ahead and make that into a string method there and I'm actually gonna go ahead and open up my admin panel and create a user so let's do four slash admin and login and you'll see we have no profiles at this point and we should have one user so we have dentist but no profile attached so I'll go ahead and add a new user and we'll just call this user Tim not time and go ahead and add a password and verify that and before I save this I'm actually printing out a profile created and profile updated depending on which one is triggered so we should see that here so let's go ahead and save that and let's see if that method ran I actually don't know there it is so profile was created and if I go to my profiles here there we go we see Tim he's attached to the user so that's an awesome way to make that connection without having to write this logic to in multiple times and in everywhere that we need this so this can work for the registration page or just working within the admin panel that you saw so if we actually update Tim and again I'm not gonna focus too much on the logic because you should use this however you need it but if we update Tim and go ahead and save that we will see that print out so we'll see update profile was corrupt a profile was ran so if I go ahead and save that profile was updated so I actually need to create a profile for Dennis eventually and that's actually why I had that logic in earlier but I will get an error if I try to save this because that logic right now is just gonna look for the profile instance which we don't have one so it's gonna give us a bug and I just want to show you that before you run into that later by yourself without fixing it so there we go it has no app should be profile and that's what we fixed earlier so let's go ahead and change up the way that we can build these receiver connections there's two ways that the documentation tells us we can make this connection and the first one was just doing this post save dot connect but another way we can go about this and I honestly don't know if there's a difference between these two but we could also use decorator so I'm going to comment these out and now we just disconnected the receiver so nothing's gonna happen on save but let's just go ahead and run an import for that decorator so we're gonna do from Jango dot dispatch and we're gonna import a receiver so receiver and this decorator now can just be put above both of these receivers and let's go ahead and pass it a few parameters so we're gonna tell it to run on post save and give it the sender and that's gonna be that user so it's not different at all really it's the same exact thing but just in a decorator format and again I looked through the documentation I couldn't find what the difference was but you're gonna see people use both of these methods in fact I think I've seen more people use the decorators there so to test this let's go ahead and open up this right here it looks like I ran the wrong import maybe I misspelled something give me a second to fix this I'm gonna go ahead and just copy and paste some code and I think it'll fix that yeah I spell receiver wrong there that was the issue so if I open that up again and I'll just go ahead and create and I spelt it obviously wrong on the decorator too sure you notice that so I'm gonna create the user from the command prompt so let's just do this let's do Python manage dot py create super user and we'll just call this user Eric and just give us some kind of email to Eric at gmail.com and get a password and there we go profile was creative and if I log into the admin panel if I turn on the server we'll see that profile so the same exact thing happened with a decorator so what I want to do now is actually go ahead and create a file called signals dot py and store everything in there and once we move it we're gonna have to create one more little configuration to make sure everything works so my app is called base and in here we'll just do signals py and again this is just how the Django documentation recommends we do all this and I'll take all of that code and copy it and paste it in here and for models I'm going to go ahead and steal that post save and receiver import or the decorator and we'll just go ahead and put those up here and then I need to import two other things here so I need to take this user model and also import all of my models here so let's go ahead and pass that in and then do from models import take profile so right now this is the same exact structure we had in our models up UI file but we need to configure these in the apps config file here so this is actually something that I struggled with and I couldn't figure out why my code wasn't working but we need to override the ready method and we're the config files ready method why can't I spell ready there we go and we'll pass himself and we're just gonna import base so my app name is base and we'll do dot signals so we'll import signals and one thing I noticed here is this code will not run if and I don't know if anyone else ran into this here but this code won't run if our imports are done we're in Django you could just do something like that just the app name we had to reference the config file in order to make that work so I'll actually show you a workaround that but I don't know if it's the best method I would just recommend to you point everything to the apps config file so if I go here and create this user so Python managed py and we'll do create super user and we'll just create this user create super user there we go and we'll call this user Mike and then we'll do Mike at gmail.com and I'll go ahead and give him a password confirm it so there we go profile was created and our signals are in signals py so we moved everything into here configured it in our apps config file here so it's within your app and right here not yeah there's apps here and we just added it into this ready method here and we also had to make sure that our installed apps were set like this so here's the workaround and again I'm not sure if this is the best method but I'm gonna go ahead and just copy and paste some code and I'll show you what you can do is you can actually add it to the init file here if you don't do this right here and if you also just call your app like this so I don't know if that's the best method but this did work and I'm gonna comment that out and actually leave it in the source code in the video description so this is only one type of signal that we just work with but if you go to the Django Docs you'll see a lot of different methods and I won't cover those right now I might do another video where we do a full mini crash course on these but if you look at the signals here and look at the built-in ones we have our model signals which have pre saved posts a which is what we used pre save will actually call the signal before the model is saved so maybe you want to run some kind of check you can do some after deletions after many-to-many field changes and then the really cool ones that i personally saw where the request in response signal so that's why I wouldn't mind actually doing another video on just covering these so these are basically whenever a user visits a page so if you're trying to track page visits frequencies of a user go into a page you can actually have some kind of analytics on your site and record this information so signals can do a lot of stuff for us and it's a really cool topic that I actually haven't really torn apart myself yet I really want to dig into it and understand them better but for now this is it and in the next video if you're part of my crash-course series will go ahead and actually apply this into our project
Info
Channel: Dennis Ivy
Views: 62,269
Rating: undefined out of 5
Keywords: Programming, Software Developer, Dennis Ivy, Dennis Ivanov, Django, Django Signals, post_save, pre_save, senders, recievers, python
Id: Kc1Q_ayAeQk
Channel Id: undefined
Length: 13min 27sec (807 seconds)
Published: Mon Jan 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.