django-signals

Django: Signal on queryset.update

一曲冷凌霜 提交于 2019-12-22 03:55:14
问题 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() ? 回答1: 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

How to get the request object in django post_save listener

泄露秘密 提交于 2019-12-22 01:30:07
问题 @receiver(post_save, sender=StudentActionModel) def save_student_activity(sender, instance, **kwargs): # update the model object with some info from the request object instance.came_from = request.REQUEST.get('request_came_from') instance.save() The user story: An user clicks somewhere, and we are recording his action. Can we, somehow, get access to the original request object so we will be able to extract some required information from it? The catch: We cannot change the StudentActionModel

Django notification on comment submission

穿精又带淫゛_ 提交于 2019-12-21 20:20:12
问题 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

Django - how do I _not_ dispatch a signal?

可紊 提交于 2019-12-21 19:19:42
问题 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

django m2m_changed with custom through model

一个人想着一个人 提交于 2019-12-21 09:18:37
问题 In Django I do have two models "Author" and "Publication" that are connected with a Many-to-Many-Field, so that I can assign different authors to a publication. Additionally, I have to use a custom through-model "Authorship" to define the correct order. class Author(models.Model): first_name = models.CharField(max_length=48) ..... class Authorship(models.Model): author = models.ForeignKey(Author) publication = models.ForeignKey('Publication') order_of_authorship = models.IntegerField(default

Is there a way to list Django signals?

非 Y 不嫁゛ 提交于 2019-12-20 11:21:10
问题 Is there a way to see which signals have been set in Django? 回答1: It's not really exposed in docs but Signal is just a class that contains a list of receivers which are called on event. You can manually check this list: from django.db.models.signals import * for signal in [pre_save, pre_init, pre_delete, post_save, post_delete, post_init, post_syncdb]: # print a List of connected listeners print signal.receivers 回答2: There's a django app called django-debug-toolbar which adds a little toolbar

How To Run Arbitrary Code After Django is “Fully Loaded”

六月ゝ 毕业季﹏ 提交于 2019-12-19 05:07:17
问题 I need to perform some fairly simple tasks after my Django environment has been "fully loaded". More specifically I need to do things like Signal.disconnect() some Django Signals that are setup by my third party library by default and connect my own Signals and I need to do some "monkey patching" to add convenience functions to some Django models from another library. I've been doing this stuff in my Django app's __init__.py file, which seems to work fine for the monkey patching, but doesn't

django post_save signals on update

纵饮孤独 提交于 2019-12-18 11:51:00
问题 I am trying to set up some post_save receivers similar to the following @receiver(post_save, sender=Game, dispatch_uid='game_updated') def game_updated(sender, **kwargs): '''DO SOME STUFF HERE''' MyPick.objects.filter(week=game.week, team=game.home_team).update(result=home_result) MyPick.objects.filter(week=game.week, team=game.away_team).update(result=away_result) @receiver(post_save, sender=MyPick, dispatch_uid='user_pick_updated') def update_standings(sender, **kwargs): '''DO STUFF''' The

Save the related objects before the actual object being edited on django admin

夙愿已清 提交于 2019-12-17 18:38:23
问题 Is it possible to save the related objects before the actual object being edited on a django admin form? For example: in models.py class Parent(model.Model): pass class Child(model.Model): parent = models.ForeignKey(Parent) @receiver(post_save,sender = Parent) def notify_parent_save(sender, instance=None, **kwargs): print "Parent save" @receiver(post_save,sender = Child) def notify_child_save(sender, instance=None, **kwargs): print "Child saved" in admin.py class ChildInline(admin

Disconnect signals for models and reconnect in django

僤鯓⒐⒋嵵緔 提交于 2019-12-17 18:22:48
问题 I need make a save with a model but i need disconnect some receivers of the signals before save it. I mean, I have a model: class MyModel(models.Model): ... def pre_save_model(sender, instance, **kwargs): ... pre_save.connect(pre_save_model, sender=MyModel) and in another place in the code i need something like: a = MyModel() ... disconnect_signals_for_model(a) a.save() ... reconnect_signals_for_model(a) Because i need in this case, save the model without execute the function pre_save_model.