I have a django-polymorphic model and want to implement a post_save signal to automatically create a related model that is also polymorphic.
It's something like the code below, the relevant piece of non-working code being the #receiver decorated work_post_save method. The problem is the instance is a ctype_id and not an object.
from django.db import models
from django.db.models import signals
from django.dispatch import receiver
from polymorphic.models import PolymorphicModel
from mygallery.models import Album
# Work Parent PolymorphicModel
class Work(PolymorphicModel):
title = models.CharField(blank=True,max_length=256)
slug = models.SlugField(max_length=256)
#receiver(signals.post_save, sender=Work)
def work_post_save(sender, instance, signal, created, **kwargs):
album, new = Album.objects.get_or_create(title=instance.title + ' Stills', slug=instance.slug + '-stills')
work_album, new = WorkAlbum.objects.get_or_create(work=instance, album=album, is_key=True)
class ArtProject(Work):
manifesto = models.CharField(blank=True,max_length=256)
class CodeProject(Work):
code = models.CharField(blank=True,max_length=256)
# Content Parent PolymorphicModel
class WorkContent(PolymorphicModel):
is_key = models.BooleanField(default=False, unique=True, default=False)
class WorkAlbum(WorkContent):
work = models.ForeignKey(Work, related_name='work_albums')
album = models.ForeignKey(Album, related_name='album_works')
I've only fiddled around with this for a bit so I'm not 100% sure what the correct way of handling this is.
What I ended up doing is to not declaring the sender in the #receiver annotation. This has the effect that the callback is triggered by every post_save signal. Then in the callback I check isinstance() with my parent model (in your case Work), so that the callback is only executed after a model I'm interested in is saved. When the callback is executed, the instance parameter is a child model (in your case ArtProject or CodeProject).
#receiver(signals.post_save)
def work_post_save(sender, instance, signal, created, **kwargs):
if isinstance(instance, Work):
# instance is either ArtProject or CodeProject
album, new = Album.objects.get_or_create(title=instance.title + ' Stills', slug=instance.slug + '-stills')
work_album, new = WorkAlbum.objects.get_or_create(work=instance, album=album, is_key=True)
Triggering directly on the parent save() is apparently not supported.
Related
I've got a question regarding django signals.
Let's say I have these models:
class Parent(models.Model):
parent_name = (...)
class Children(models.Model):
child_name = (...)
parent = models.ForeignKey(Parent, on_delete=models.CASCADE, related_name='children')
And let's assume I have this signal connected to the post_save signal of Parent class:
def handle(*args, **kwargs):
(...)
post_save.connect(handle, sender=Parent)
Now, If I create a new child:
some_parent = Parent.objects.get(...)
new_child = Child.objects.create(
...,
parent = some_parent
)
Even though I'm just creating a new Child, Django will send a post_save signal from some_parent and thus handle will be invoked. Is there a way to ignore this signal? Something similar to this:
def handle(*args, **kwargs):
if <some_condition>: # check if the signal is sent just because a new child is created
# Ignore the signal
return
# Do everything as usual
...
Sure thing, the first args to the post_save handler are instance and created.
#receiver(models.signals.post_save, sender=Parent)
def handle(instance, created, **kwargs):
if created:
return
do_thingamy()
I would like to get the absolute url from my saved object. My model has a method named get_absolute_url and I would like to call this method with my django post_save signal.
I receive a post_save signal when a new entry is added inside a specific table named Thread. This post_save signal executes my Celery task.
My Thread model is :
class Thread(models.Model):
""" A thread with a title """
topic = models.ForeignKey('Topic')
title = models.CharField(max_length=200)
sticky = models.BooleanField(default=False)
slug = models.SlugField()
time_created = models.DateTimeField(default=timezone.now)
time_last_activity = models.DateTimeField(default=timezone.now)
def __init__(self, *args, **kwargs):
""" Initialize 'time_last_activity' to 'time_created' """
super(Thread, self).__init__(*args, **kwargs)
self.time_last_activity = self.time_created
def __str__(self):
""" Return the thread's title """
return self.title
def get_absolute_url(self):
""" Return the url of the instance's detail view """
url_kwargs = {
'topic_pk': self.topic.pk,
'topic_slug': self.topic.slug,
'thread_pk': self.pk,
'thread_slug': self.slug,
}
return reverse('simple-forums:thread-detail', kwargs=url_kwargs)
In my model, I have a celery.py file :
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db.models.signals import post_save
from django.dispatch import receiver
from simple_forums.models import Thread
from ..tasks import thread_notification
#receiver(post_save, sender=Thread)
def get_new_thread(sender, instance, **kwargs):
""" Post_save method which start Celery task to notify forum subscribers that a new thread has been created """
url = Thread.get_absolute_url()
print(url)
thread_title = instance.title
thread_id = instance.id
topic_id = instance.topic_id
topic_slug = instance.topic.slug
topic_title = instance.topic.title
thread_notification.delay(thread_id=thread_id, thread_title=thread_title, topic_id=topic_id, topic_slug=topic_slug,
topic_title=topic_title)
And in my tasks.py file :
# -*- coding: utf-8 -*-
from celery import shared_task
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _
User = get_user_model()
#shared_task(bind=True, time_limit=3600, soft_time_limit=3600)
def thread_notification(self):
print('Celery task executed')
return ['success_message', _('Celery task ended')]
I would like to get the absolute_url in order to send an email with the new Thread path.
My question is : How I can pick up get_absolute_url or use request.build_absolute_uri if I don't have a specific view (not necessary) ?
Here:
#receiver(post_save, sender=Thread)
def get_new_thread(sender, instance, **kwargs):
url = Thread.get_absolute_url()
the saved Thread instance is (suprise, surprise) your instance argument, so you want:
url = instance.get_absolute_url()
calling an instance method on a class makes no sense (nb: except for a couple specific corner cases, and then you have to pass the instance as first argument, but let's not get further with this, when you'll need it you'll know how it works).
Now since you're in the same app, using a signal here makes no sense either and is actually an antipattern. The point of signals is to allow an app to react to events emitted by other apps. Here, your code should quite simply be in Thread.save().
I am using django 2.0.8 and Python 3.5. I want to be able to send and receive custom signals when an object is saved to the database.
I have followed the Django documentation on listening to signals and also the core signals bundled with Django - however, I am unable to get my example to work.
This is what I have so far:
myapp/models.py
from django.db import models
import django.dispatch
my_signal = django.dispatch.Signal(providing_args=["name"])
class Foo(models.Model):
name = models.CharField(max_length=16)
def save(self, *args, **kwargs):
try:
# Call the "real" save() method.
super().save(*args, **kwargs)
# Fire off signal
my_signal.send(sender=self.__class__, name=self.name)
except Exception as e:
print ('Exception:', e)
#pass
myapp/signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Foo
#receiver(post_save, sender=Foo)
def foo_handler(sender, **kwargs):
print('Foo Signal Recieved!')
print(kwargs)
myapp/app.py
class MyappConfig(AppConfig):
name = 'myapp'
label = 'myapp'
def ready(self):
import myapp.signals
Sample usage
from myapp.models import Foo
foo = Foo(name='Homer Simpson')
foo.save() # Object is saved, but event is not fired!
Can anyone explain why the signal is not being fired?
It seems you need two features supplied by Django. signal and contenttypes.
So read the doc first
The model Activity is related to contenttypes,seems you miss object_id Field, which indicate which model instance is being crud.
For every crud action, An Activity instance is being created.This part is just the code written in signal.py
signal: signal have to connect each concrete model. Fortunately,See the source code of decorator receiver.
We have a signal list [post_save,post_delete] and a model list (FoodooChile, FooBarChile) to connect .
In post_save,argument created indicate the action is create or update.
At last, Usually we import the signal file in urls.py, maybe not the best practice.
It is also related to your settings.py. use 'myapp.apps.MyappConfig' replace myapp in settings.py,or define default_app_config = 'myapp.apps.MyappConfig' in myapp/__init__.py. The link above in comments describe this in detail
In the myapp.signals you have a receiver that handels the post_save signal (#receiver(post_save, sender=Foo)) it doesn't connect to your signal.
Make sure you are using your app config in the __init__.py of you application default_app_config = 'myapp.apps.MyappConfig'
To connect to the signal you created try this in your signals.py file:
#receiver(my_signal)
def my_handler(name, **kwargs):
print(name)
You are reinventing the wheel, but only putting it on one side of the cart, so to speak.
the post_save signal is always sent on save, so defining your own signal is overkill. I know you got the argument there, but the receiver has the sender argument already, which is the saved object, so you can just do sender.name and you got the value you need.
Apart from that, you have a syntax error, your custom save function for your model is not indented. I don't know if this is a formatting error in your question or if that is how it looks in your code. Either way, should work if you just drop your custom signal.
Model
from django.db import models
import django.dispatch
class Foo(models.Model):
name = models.CharField(max_length=16)
Signals
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Foo
#receiver(post_save, sender=Foo)
def foo_handler(sender, **kwargs):
print(sender.name)
App
class MyappConfig(AppConfig):
name = 'myapp'
label = 'myapp'
def ready(self):
import myapp.signals
Sample
from myapp.models import Foo
foo = Foo(name='Homer Simpson')
foo.save()
How to rewrite the Django model save method?
class Message(models.Model):
"""
message
"""
message_num = models.CharField(default=getMessageNum, max_length=16)
title = models.CharField(max_length=64)
content = models.CharField(max_length=1024)
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
# I want send email there
pass
I mean, in the Django model, if I create instance success, I want to call a function, such as send a email in the function.
I find in the Django model have a save method. I am not sure whether should write other code, because there are so many params.
I mean whether I only should care about my send email logic?
When you override the save method, you still have to make sure that the it actually saves the instance. You can do that by simply calling the parent class' save via super:
class Message(models.Model):
# ...
def save(self, *args, **kwargs):
# this will take care of the saving
super(Message, self).save(*args, **kwargs)
# do email stuff
# better handle ecxeptions well or the saving might be rolled back
You can also connect the mail sending to the post_save (or pre_save, depending on your logic) signal. Whether you want to separate one orm the other in that way depends on how closely the two actions are linked and a bit on your taste.
Overriding save gives you the option to intervene in the saving process, e.g. you can change the value of fields based on whether the mail sending was successful or not save the instance at all.
The solution to what you want to do is to use Django Signals. By using Signals you can hook code to when a model is created and saved without having to rewrite the save method, that keep the separation of code and logic in a much nicer way, obviously the model does not need to know about the emails for example.
An example of how to use Signals would be to simply do the following:
from django.db.models.signals import pre_save
from django.dispatch import receiver
from myapp.models import MyModel
#receiver(pre_save, sender=MyModel)
def my_handler(sender, **kwargs):
# Code to execute whenever MyModel is saved...
If you still want to override the save() method you can use the Python super() method to do so (docs).
class MyModel(models.Model):
def save(self, *args, **kwargs):
# This will call the parent method that you are overriding
# so it will save your instance with the default behavior.
super(MyModel, self).save(*args, **kwargs)
# Then we add whatever extra code we want, e.g. send email...
Messenger.send_email()
You need to activate signal once your message is saved. That means, when your message is saved, django will issue signal as follows:
from django.db.models.signals import post_save
from django.dispatch import receiver
class Message(models.Model):
# fields...
# method for sending email
#receiver(post_save, sender=Message, dispatch_uid="send_email")
def send_email(sender, instance, **kwargs):
# your email send logic here..
You can put your signals in signals.py file inside your app folder and make sure to import that in your application config file as follows:
message/apps.py
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'message'
def ready(self):
import message.signals
And update init file as follows:
message/__init__.py
default_app_config = 'message.apps.MyAppConfig'
I have a few model inheritance levels in Django:
class WorkAttachment(models.Model):
""" Abstract class that holds all fields that are required in each attachment """
work = models.ForeignKey(Work)
added = models.DateTimeField(default=datetime.datetime.now)
views = models.IntegerField(default=0)
class Meta:
abstract = True
class WorkAttachmentFileBased(WorkAttachment):
""" Another base class, but for file based attachments """
description = models.CharField(max_length=500, blank=True)
size = models.IntegerField(verbose_name=_('size in bytes'))
class Meta:
abstract = True
class WorkAttachmentPicture(WorkAttachmentFileBased):
""" Picture attached to work """
image = models.ImageField(upload_to='works/images', width_field='width', height_field='height')
width = models.IntegerField()
height = models.IntegerField()
There are many different models inherited from WorkAttachmentFileBased and WorkAttachment. I want to create a signal, which would update an attachment_count field for parent work, when attachment is created. It would be logical, to think that signal made for parent sender (WorkAttachment) would run for all inherited models too, but it does not. Here is my code:
#receiver(post_save, sender=WorkAttachment, dispatch_uid="att_post_save")
def update_attachment_count_on_save(sender, instance, **kwargs):
""" Update file count for work when attachment was saved."""
instance.work.attachment_count += 1
instance.work.save()
Is there a way to make this signal work for all models inherited from WorkAttachment?
Python 2.7, Django 1.4 pre-alpha
P.S. I've tried one of the solutions I found on the net, but it did not work for me.
You could register the connection handler without sender specified. And filter the needed models inside it.
from django.db.models.signals import post_save
from django.dispatch import receiver
#receiver(post_save)
def my_handler(sender, **kwargs):
# Returns false if 'sender' is NOT a subclass of AbstractModel
if not issubclass(sender, AbstractModel):
return
...
Ref: https://groups.google.com/d/msg/django-users/E_u9pHIkiI0/YgzA1p8XaSMJ
The simplest solution is to not restrict on the sender, but to check in the signal handler whether the respective instance is a subclass:
#receiver(post_save)
def update_attachment_count_on_save(sender, instance, **kwargs):
if isinstance(instance, WorkAttachment):
...
However, this may incur a significant performance overhead as every time any model is saved, the above function is called.
I think I've found the most Django-way of doing this: Recent versions of Django suggest to connect signal handlers in a file called signals.py. Here's the necessary wiring code:
your_app/__init__.py:
default_app_config = 'your_app.apps.YourAppConfig'
your_app/apps.py:
import django.apps
class YourAppConfig(django.apps.AppConfig):
name = 'your_app'
def ready(self):
import your_app.signals
your_app/signals.py:
def get_subclasses(cls):
result = [cls]
classes_to_inspect = [cls]
while classes_to_inspect:
class_to_inspect = classes_to_inspect.pop()
for subclass in class_to_inspect.__subclasses__():
if subclass not in result:
result.append(subclass)
classes_to_inspect.append(subclass)
return result
def update_attachment_count_on_save(sender, instance, **kwargs):
instance.work.attachment_count += 1
instance.work.save()
for subclass in get_subclasses(WorkAttachment):
post_save.connect(update_attachment_count_on_save, subclass)
I think this works for all subclasses, because they will all be loaded by the time YourAppConfig.ready is called (and thus signals is imported).
You could try something like:
model_classes = [WorkAttachment, WorkAttachmentFileBased, WorkAttachmentPicture, ...]
def update_attachment_count_on_save(sender, instance, **kwargs):
instance.work.attachment_count += 1
instance.work.save()
for model_class in model_classes:
post_save.connect(update_attachment_count_on_save,
sender=model_class,
dispatch_uid="att_post_save_"+model_class.__name__)
(Disclaimer: I have not tested the above)
I just did this using python's (relatively) new __init_subclass__ method:
from django.db import models
def perform_on_save(*args, **kw):
print("Doing something important after saving.")
class ParentClass(models.Model):
class Meta:
abstract = True
#classmethod
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
models.signals.post_save.connect(perform_on_save, sender=cls)
class MySubclass(ParentClass):
pass # signal automatically gets connected.
This requires django 2.1 and python 3.6 or better. Note that the #classmethod line seems to be required when working with the django model and associated metaclass even though it's not required according to the official python docs.
post_save.connect(my_handler, ParentClass)
# connect all subclasses of base content item too
for subclass in ParentClass.__subclasses__():
post_save.connect(my_handler, subclass)
have a nice day!
Michael Herrmann's solution is definitively the most Django-way of doing this.
And yes it works for all subclasses as they are loaded at the ready() call.
I would like to contribute with the documentation references :
In practice, signal handlers are usually defined in a signals submodule of the application they relate to. Signal receivers are connected in the ready() method of your application configuration class. If you’re using the receiver() decorator, simply import the signals submodule inside ready().
https://docs.djangoproject.com/en/dev/topics/signals/#connecting-receiver-functions
And add a warning :
The ready() method may be executed more than once during testing, so you may want to guard your signals from duplication, especially if you’re planning to send them within tests.
https://docs.djangoproject.com/en/dev/topics/signals/#connecting-receiver-functions
So you might want to prevent duplicate signals with a dispatch_uid parameter on the connect function.
post_save.connect(my_callback, dispatch_uid="my_unique_identifier")
In this context I'll do :
for subclass in get_subclasses(WorkAttachment):
post_save.connect(update_attachment_count_on_save, subclass, dispatch_uid=subclass.__name__)
https://docs.djangoproject.com/en/dev/topics/signals/#preventing-duplicate-signals
This solution resolves the problem when not all modules imported into memory.
def inherited_receiver(signal, sender, **kwargs):
"""
Decorator connect receivers and all receiver's subclasses to signals.
#inherited_receiver(post_save, sender=MyModel)
def signal_receiver(sender, **kwargs):
...
"""
parent_cls = sender
def wrapper(func):
def childs_receiver(sender, **kw):
"""
the receiver detect that func will execute for child
(and same parent) classes only.
"""
child_cls = sender
if issubclass(child_cls, parent_cls):
func(sender=child_cls, **kw)
signal.connect(childs_receiver, **kwargs)
return childs_receiver
return wrapper
It's also possible to use content types to discover subclasses - assuming you have the base class and subclasses packaged in the same app. Something like this would work:
from django.contrib.contenttypes.models import ContentType
content_types = ContentType.objects.filter(app_label="your_app")
for content_type in content_types:
model = content_type.model_class()
post_save.connect(update_attachment_count_on_save, sender=model)
In addition to #clwainwright answer, I configured his answer to instead work for the m2m_changed signal. I had to post it as an answer for the code formatting to make sense:
#classmethod
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
for m2m_field in cls._meta.many_to_many:
if hasattr(cls, m2m_field.attname) and hasattr(getattr(cls, m2m_field.attname), 'through'):
models.signals.m2m_changed.connect(m2m_changed_receiver, weak=False, sender=getattr(cls, m2m_field.attname).through)
It does a couple of checks to ensure it doesn't break if anything changes in future Django versions.