django-signals

Django cascade delete and post_delete signal

℡╲_俬逩灬. 提交于 2021-02-18 11:55:08
问题 In my application post_delete signals being recorded in a specific model and when it was removed. class A(models.Model): ... class B(models.Model): a = models.ForeignKey('A') class C(models.Model): b = models.ForeignKey('B') def log_delete(sender, instance, **kwargs): logging post_delete.connect(log_delete, sender = A) post_delete.connect(log_delete, sender = C) When you delete an instance of A cascade delete occurs removing B and C instances. How can I disable signal for child instances on

Django signals from admin

此生再无相见时 提交于 2021-02-16 09:24:10
问题 How can I distinguish between, for example, a post_save that comes from a regular view and one that comes from the admin? 回答1: By overriding ModelAdmin.response_add you may get a similar functionality, as it seems that the django admin does not sent any signals. The response_add function gets called after it successfully validated and added all data, such as related fields but also the object itself. So by overriding the response_add method in our own ModelAdmin class we can execute code

How to make sum query with type casting and calculation in django views?

孤街醉人 提交于 2021-02-11 13:50:10
问题 I'm calculating the sold items cost in django views and in django signals, and I want to calculate sold items cost on the fly. Price and quantity fields are integers. How can I convert one of them to the float and make sum query with some calculations like these sql queries below? SELECT sum((t.price::FLOAT * t.quantity) / 1000) as cost FROM public."sold" t; SELECT t.id, t.price, t.quantity, sum((price::FLOAT * quantity) / 1000) as cost FROM public."sold" t GROUP BY t.id; EDIT: Of course

django signal disconnect not working

旧城冷巷雨未停 提交于 2021-02-07 04:18:04
问题 I have a signal class where I define signal receivers class SearchSignalProcessor(object): def post_save_connector(self, sender, instance, **kwargs): # do something def pre_delete_connector(self, sender, instance, **kwargs): # do something def setup(self, model): signals.post_save.connect(self.post_save_connector, sender=model, dispatch_uid="index_after_save") signals.pre_delete.connect(self.pre_delete_connector, sender=model, dispatch_uid="index_before_delete") def teardown(self, model):

django signal disconnect not working

匆匆过客 提交于 2021-02-07 04:14:26
问题 I have a signal class where I define signal receivers class SearchSignalProcessor(object): def post_save_connector(self, sender, instance, **kwargs): # do something def pre_delete_connector(self, sender, instance, **kwargs): # do something def setup(self, model): signals.post_save.connect(self.post_save_connector, sender=model, dispatch_uid="index_after_save") signals.pre_delete.connect(self.pre_delete_connector, sender=model, dispatch_uid="index_before_delete") def teardown(self, model):

Sending emails when a user is activated in the Django admin

廉价感情. 提交于 2021-01-27 08:25:15
问题 I'm about to create a site that has monitored registration in that only certain people are allowed to register. Undoubtedly some misfits will register despite any writing I put above the registration form so we're going with moderation. Upon registration a django.contrib.auth User and profile will be created and an email will be sent to the moderator. The moderator will log into the Django admin site, check they're somebody who is allowed to register and mark their account active. If they're