I am trying to create a system which enables user to upload a zipfile, and then extract it using post_save signal.
class Project:
....
file_zip=FileField(upload_to='projects/%Y/%m/%d')
#receiver(post_save, sender=Project)
def unzip_and_process(sender, **kwargs):
#project_zip = FieldFile.open(file_zip, mode='rb')
file_path = sender.instance.file_zip.path
with zipfile.ZipFile(file_path, 'r') as project_zip:
project_zip.extractall(re.search('[^\s]+(?=\.zip)', file_path).group(0))
project_zip.close()
unzip_and_process method works fine when correct file paths are provided(in this case, i need to provide instance.file_zip.path. However, I couldn't get/set the instance with the signals. Django documentation about signals is not clear and have no examples. So, what do I do?
Actually, Django's documentation about signals is very clear and does contain examples.
In your case, the post_save signals sends the following arguments: sender (the model class), instance (the instance of class sender), created, raw, and using. If you need to access instance, you can access it using kwargs['instance'] in your example or, better, change your callback function to accept the argument:
#receiver(post_save, sender=Project)
def unzip_and_process(sender, instance, created, raw, using, **kwargs):
# Now *instance* is the instance you want
# ...
This worked for me when connecting Django Signals:
Here is the models.py:
class MyModel(models.Model):
name = models.CharField(max_length=100)
And the Signal that access it post_save:
#receiver(post_save, sender=MyModel)
def print_name(sender, instance, **kwargs):
print '%s' % instance.name
Related
I don't understand how 2 signals executed together relate to each other. For example consider the following:
from django.db import models
from django.contrib.auth.models import User
from PIL import Image
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
def __str__(self):
return f'{self.user.username} Profile'
# Overriding save method-image resizing
def save(self, **kwargs):
super().save(kwargs)
img = Image.open(self.image.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.image.path) # save() of parent class
#Signals when a user is registered his profile is automatically updated
#receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile(user=instance) #could do Profile.objects.create(user=instance) instead to replace the 2 lines
instance.profile.save() #reverse query
I understand the following:
create_profile: is my receiver function. It will do something when .save() method takes place, in our case will create a Profile instance and save it.
sender: is the Model that is actually signalling this to take place. So in our case, if we want a signal to take place when the save method is executed on User model then the sender would be the User.
post_save is the signal
Regarding my question now. I have seen the above function broken down into the two following functions.
#receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile(user=instance)
#receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save() #reverse query
I am aware it is pointless since I could do what I did but I don't understand how does my save_profile function 'remembers' the Profile instance made on create_profile?
As far as I can tell, save_profile is a separate function that has no reference to create_profile function (example by create_profile or so on the save_profile body).
I am assuming when a user instance is saved the create_profile function is executed it makes a profile object then the save_profile function is executed and for same user instance the profile object is saved,
but I still don't understand how it knows I refer to same profile made by that instance.
Why you not state it is example from Corey tutorial ?
Corey Tutorial, part 8, and if I not mistaken he didnt have to create two separate signals, because one was enough ..
To your question:
The functions are:
#receiver(post_save, sender=User)
def create_profile_handler(sender, instance, created, **kwargs):
#Run each time a new user is created, and attach to the user a profile !
# sender - The model class (User)
# instance - The actual instance being saved. (User)
# created - A boolean. 'True' if a new record was created.
if created:
Profile.objects.create(user=instance)
#'create' - initialize and save a profile and attach it to the User
# Now, lets save the profile each time the user object is been saved.
#receiver(post_save, sender=User)
def save_profile_handler(sender, instance, **kwargs):
instance.profile.save() # User.profile.save() - saves the profile
we know from the Django Docs, that create() method include save() operation in it.
So not only that the second signal is redundant (because we can do its operations in the first signal), further more - the save is a bit redundant it self (because create calls save).
I want to call a method after this object (in this example Question) is saved.
This is what I got:
class Question(models.Model):
...
def after_save(sender, instance, *args, **kwargs):
some_method(instance)
post_save.connect(Question.after_save, sender=Question)
It kind of works. But the problem is that the instance data is the old one (same as before the save). So some_method(instance) gets the old instance.
But I need the instance with the new data.
The method you call from post_save signal should be outside of the Model. You can put the method inside the models.py or in another file such as signals.py
class Question(models.Model):
...
def some_method(self):
return "hello"
def question_saved(sender, instance, *args, **kwargs):
instance.some_method()
post_save.connect(question_saved, sender=Question)
You can override save method, and call what ever you want after the object gets saved
Here is a related link: Django. Override save for model
What you want is something called signals: https://docs.djangoproject.com/en/3.2/topics/signals/.
Some operations send a signals before and after they're completed successfully. In the docs you can find a list of built-in signals. You can also create custom signals. For example assume that I want to do something specific each time a new user is created. Then my code would be something like this:
users/models.py
from django.contrib.auth.models import AbstractUser
from django.dispatch import receiver
from django.db.models.signals import post_save
class CustomUser(AbstractUser):
pass
###############
# Recieve the signals and get the signals that are sent
# after models are saved and whose sender is CustomUser
###############
#receiver(post_save, sender='CustomUser')
def do_something():
do_something_specific
You can also save your signals in a separate file called signals.py but you need to change your app's apps:
<appname>/apps.py
from django.apps import AppConfig
class AppnameConfig(AppConfig):
name = <appname>
def ready(self):
import <appname>.signals
I use the pre_save and post_save signals to send analytics to Mixpanel. I prefer to keep this separated from my model's save method.
Is there a way to save the old values of an instance when the pre_save signal occurs, and then check the new values against them on post_save?
My code looks like this:
#receiver(pre_save, sender=Activity)
def send_user_profile_analytics(sender, **kwargs):
activity_completed_old_value = kwargs['instance'].is_completed
# store this value somewhere?
#receiver(post_save, sender=Activity)
def send_user_profile_analytics(sender, **kwargs):
if kwargs['instance'].is_completed != activity_completed_old_value:
# send analytics
For me it seems more robust to use post_save to send the analytics than pre_save, but at that point I can't see what has changed in the model instance. I would like to prevent using globals or implementing something in my model's save function.
You can store them as instance attributes.
#receiver(pre_save, sender=Activity)
def send_user_profile_analytics(sender, **kwargs):
instance = kwargs['instance']
instance._activity_completed_old_value = instance.is_completed
#receiver(post_save, sender=Activity)
def send_user_profile_analytics(sender, **kwargs):
instance = kwargs['instance']
if instance.is_completed != instance._activity_completed_old_value:
# send analytics
In this way you "send analytics" only if is_completed changes during save (that means that save doesn't just store the value but makes some further elaboration).
If you want to perform an action when a field is changed during instance life-time (that is from its creation till the save) you should store the initial value during post_init (and not pre_save).
So I read the Django source code (post 1.5) that you can now register multiple multiple signals to a receiver function:
def receiver(signal, **kwargs):
"""
A decorator for connecting receivers to signals. Used by passing in the
signal (or list of signals) and keyword arguments to connect::
#receiver(post_save, sender=MyModel)
def signal_receiver(sender, **kwargs):
...
#receiver([post_save, post_delete], sender=MyModel)
def signals_receiver(sender, **kwargs):
...
"""
... implementation code...
However, I want to register multiple post_save signals from different senders to the same function. Right now, I just call
post_save.connect(fn_name, model_name)
for each model that I have. Is there a better way to do this with the new Django 1.5 #receiver decorator capability?
You can do that using the #receiver decorator:
from django.dispatch import receiver
#receiver(post_save, sender=Model1)
#receiver(post_save, sender=Model2)
#receiver(post_save, sender=Model3)
def my_signal_handle(sender , **kwargs)
# some code here
Per the Django documentation on receivers, receivers by default do not need to be connected to a specific sender. So what you're describing is default Django functionality.
In other words, to do this using the #receiver decorator you simply don't specify a sender in the decorator. For example:
#receiver(post_save) # instead of #receiver(post_save, sender=Rebel)
def set_winner(sender, instance=None, created=False, **kwargs):
list_of_models = ('Rebel', 'Stormtrooper', 'Battleground')
if sender.__name__ in list_of_models: # this is the dynamic part you want
if created: # only run when object is first created
... set the winner ...
This assumes models that look like:
class Rebel(models.Model):
...
class Stormtrooper(models.Model):
...
class Battleground(models.Model):
...
You can skip model_name and you will connect to all models post_save. Then you can filter if you are in right model in the handler:
post_save.connect(foo)
def foo(sender, **kwargs):
if sender not in [FooModel, BarModel]:
return
... actual code ...
or you can filter based on field in model:
def foo(sender, **kwargs):
if not getattr(sender, 'process_by_foo', False):
return
... actual code ...
def receiver_with_multiple_senders(signal, senders, **kwargs):
"""
Based on django.dispatch.dispatcher.receiver
Allows multiple senders so we can avoid using a stack of
regular receiver decorators with one sender each.
"""
def decorator(receiver_func):
for sender in senders:
if isinstance(signal, (list, tuple)):
for s in signal:
s.connect(receiver_func, sender=sender, **kwargs)
else:
signal.connect(receiver_func, sender=sender, **kwargs)
return receiver_func
return decorator
I'm trying to use signals post_save for the first time. I have read the documents, but still need some advice.
I'm trying to update my model field called 'charge'.
#receiver(post_save, sender=Message)
def my_handler(sender, **kwargs):
if not sender.charge:
sender(charge=sender.length(sender))
sender.save()
However, this gives the error Message' has no attribute 'charge', but charge does exist within message!
sender here is the Message class itself, not the instance that's being saved. The actual instance is passed as the keyword argument instance. Also, with post_save, if you're not careful, you'll get yourself in an infinite loop. Better to use pre_save.
#receiver(pre_save, sender=Message)
def my_handler(sender, **kwargs):
instance = kwargs['instance']
if not instance.charge:
instance.charge = instance.length()
# No need to save, as we're slipping the value in
# before we hit the database.