django-signals

Django: how can I tell if the post_save signal triggers on a new object?

不问归期 提交于 2019-12-05 08:20:48
问题 I need to do some background post-processing on newly created objects in Django. This post-processing should only run on new objects, not objects that are just updated. I know that in pre_save I can check if the object has an id, if it has not then it's a new object. But the problem is that in the post-processing I need access to the id (so that I can save the results back to the database). How can I do this in a clean way? 回答1: Have a look at docs: https://docs.djangoproject.com/en/stable

Django: Signal on queryset.update

余生长醉 提交于 2019-12-05 01:41:50
Django is sending the pre/post_delete signals if you are using the queryset.delete() method, but shouldn't it then also send pre/post_save on queryset.update() ? Josh Ourisman Perhaps it should, but it doesn't. .update() does not call the .save() method on the individual objects in the QuerySet, and instead updates the all in a single SQL call (UPDATE, as it happens). Since it doesn't use .save() , it would be inconsistent for it to call the pre- and post-save signals. I can certainly envision use-cases in which one might want it to do so, but I can also envision cases in which one wouldn't.

Django pre_save signal does not work

懵懂的女人 提交于 2019-12-05 01:31:36
I tested the "pre_save" signal of Django in the following ways, but cannot catch the signal in either of them. $ from django.db.models.signals import pre_save import logging def my_callback(sender, **kwargs): logging.debug("======================================") pre_save.connect(my_callback) Run the above code in manage.py shell: Then I run my website and see models.save() work successfully, but the callback function does not run. Alternatively, I run the above code on shell again and then run models.save() in the shell. "save" works well again but still nothing happens to the callback

Raise 404 and continue the URL chain

谁说胖子不能爱 提交于 2019-12-04 22:16:24
I've got a URLs pattern like this: urlpatterns = ( url(r'^$', list_titles, name='list'), url(r'^(?P<tag>[a-z\-0-9]+?)/$', list_titles, name='filtered-list'), url(r'^(?P<title>\S+?)/$', show_title, name='title'), ) The filtered-list and title match the same things. If there is an available list of things matching the tag in filtered-list , I want list_titles to fire off. But if there isn't a matching tag , I want to bubble that back to the URL processor so show_title fires off. If there's no matching title, I'll raise a proper 404 there. I know I can do this from inside the view...but it's a

Simple form not validating

橙三吉。 提交于 2019-12-04 15:38:17
I have found here on stackoverflow a method to extend django's built-in authentication using signals. My base User is defined by 'email' and passwords (so no username there). So I'm trying to modify it to my needs, but I'm geting a validation error for my form. Strange thing is that error is connected to the User.email field and I'm getting 'already in use' even though I'm just registering at the moment. Is it trying to save it 2 times or what ? I've discovered it when I was sending dictionary with data to form's contstructor in shell: form = MyForm(data={}) . After this form was still invalid

Django notification on comment submission

雨燕双飞 提交于 2019-12-04 12:40:19
I am making use of Django's contrib.comments and want to know the following. Are there any utils or app out there that can be plugged into an app that sends you a notification when a comment is posted on an item? I haven't really worked with signals that much, so please be a little bit descriptive. This is what I came up with. from django.contrib.comments.signals import comment_was_posted from django.core.mail import send_mail if "notification" in settings.INSTALLED_APPS: from notification import models as notification def comment_notification(request): user = request.user message = "123"

Django - how do I _not_ dispatch a signal?

烂漫一生 提交于 2019-12-04 09:43:54
I wrote some smart generic counters and managers for my models (to avoid select count queries etc.). Therefore I got some heavy logic going on for post_save. I would like to prevent handling the signal when there's no need to. I guess the perfect interface would be: instance.save(dispatch_signal=False) How can I accomplish this? Update More information about what I'm doing, if anyone's interested: Generic counters are stored in a separate table Every time Django paginates an object list, it calls overriden count() method of my custom manager, which basically retrieves the static counter value

Using Pre_delete Signal in django

天涯浪子 提交于 2019-12-04 09:21:18
问题 In my app I want to keep a track of all the questions that are being deleted. And so I have created a class(table) as such in my models file. class Deleted(models.Model): question = models.IntegerField(null=True, blank=True)#id of question being deleted user = models.IntegerField(null=True, blank=True)#id of user deleting the question dt = models.DateTimeField(null=True, blank=True)#time question is deleted When a user tries to delete a question This delete function is called: def delete

Django: determine which user is deleting when using post_delete signal

穿精又带淫゛_ 提交于 2019-12-04 06:51:24
I want admins to be notified when certain objects are deleted but I also want to determine which user is performing the delete. Is it possible? This is the code: # models.py # signal to notify admins when nodes are deleted from django.db.models.signals import post_delete from settings import DEBUG def notify_on_delete(sender, instance, using, **kwargs): ''' Notify admins when nodes are deleted. Only for production use ''' if DEBUG: #return False pass # prepare context context = { 'node': instance, 'site': SITE } # notify admins that want to receive notifications notify_admins(instance, 'email

Django Signal via Decorator on Model Method?

喜你入骨 提交于 2019-12-04 03:26:01
I'm trying to do something like these proposed signal decorators . In addition to having a decorator that connects the decorated method to a signal (with the signal's sender as an argument to the decorator), I would like to use the decorator on class methods. I'd like to use the decorator like so: class ModelA(Model): @connect.post_save(ModelB) @classmethod def observe_model_b_saved(cls, sender, instance, created, **kwargs): # do some stuff pass The decorator is: from django.db.models import signals def post_save(sender): def decorator(view): signals.post_save.connect(sender=sender, receiver