django-signals

post_save signal isn't called

老子叫甜甜 提交于 2019-11-30 08:19:10
I've already read all related questions. I have two Django projects, and signals work fine in one, but do not work in second one (I've just copy-pasted code and changed names respectively). I have an orders app with Order model. App is included in INSTALLED_APPS setting. I have app config in apps.py: from django.apps import AppConfig class OrdersConfig(AppConfig): name = 'orders' def ready(self): super(OrdersConfig, self).ready() # noinspection PyUnresolvedReferences import signals __init__.py : default_app_config = 'orders.apps.OrdersConfig' And, finally, signals.py: @receiver(post_save,

Prevent delete in Django model

筅森魡賤 提交于 2019-11-30 06:47:16
I have a setup like this (simplified for this question): class Employee(models.Model): name = models.CharField(name, unique=True) class Project(models.Model): name = models.CharField(name, unique=True) employees = models.ManyToManyField(Employee) When an Employee is about to get deleted, I want to check whether or not he is connected to any projects. If so, deletion should be impossible. I know about signals and how to work them. I can connect to the pre_delete signal, and make it throw an exception like ValidationError . This prevents deletion but it is not handled gracefully by forms and

RemovedInDjango19Warning: Model doesn't declare an explicit app_label

大城市里の小女人 提交于 2019-11-30 03:20:34
问题 Have gone through Django 1.9 deprecation warnings app_label but answers couldn't fix my problem, so asking again. I have an app that is added to INSTALLED_APPS in settings. when ever I run manage.py runserver , I get this warning, [trimmed path to project]/catalog/models.py:9: RemovedInDjango19Warning: Model class catalog.models.Category doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will

How to delete files from filesystem using post_delete - Django 1.8

随声附和 提交于 2019-11-30 02:37:18
问题 I have a model - Product, which contains a thumbnail image. I have another model which contains images associated with the product - ProductImage. I want to delete both the thumbnail and the images from the server when the product instance is deleted, and for a while this seemed to worked, but not anymore. Relevant code... Class Product(models.Model): title = Charfield thumbnail = ImageField(upload_to='thumbnails/', verbose_name='thumbnail', blank=True, ) Class ProductImage(models.Model):

How to use Django model inheritance with signals?

谁都会走 提交于 2019-11-29 20:46:43
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

Why does Django post_save signal give me pre_save data?

a 夏天 提交于 2019-11-29 13:46:35
Im trying to connect a "Information" object to many "Customers" (see code below) When one Information object is updated, I want to send email to each Customer that is connected to the Information. However, when I log the sold_to field that the signal recieves I always get what the data is like BEFORE the save. I'm guessing this is because its ManyToManyField and the data is stored in a separate table, but shouldn't the post_save signal be called after all relations have been updated? Anyone got a suggestion for a solution? class Customer name = models.CharField(max_length=200) category =

post_save signal isn't called

无人久伴 提交于 2019-11-29 12:00:32
问题 I've already read all related questions. I have two Django projects, and signals work fine in one, but do not work in second one (I've just copy-pasted code and changed names respectively). I have an orders app with Order model. App is included in INSTALLED_APPS setting. I have app config in apps.py: from django.apps import AppConfig class OrdersConfig(AppConfig): name = 'orders' def ready(self): super(OrdersConfig, self).ready() # noinspection PyUnresolvedReferences import signals __init__

A signal m2m_changed and bug with post_remove

删除回忆录丶 提交于 2019-11-29 10:38:22
I need to detect a post_remove signal, so I have written : def handler1(sender, instance, action, reverse, model, pk_set, **kwargs): if (action == 'post_remove'): test1() # not declared but make a bug if it works, to detect :) m2m_changed.connect(handler1, sender=Course.subscribed.through) If I change 'post_remove' by 'post_add' it is ok.. Is it a django's bug about post_remove ?? I use that model and I switch beetween two values of 'subscribed' (so one added and one deleted) class Course(models.Model): name = models.CharField(max_length=30) subscribed = models.ManyToManyField(User, related

How can I have a Django signal call a model method?

此生再无相见时 提交于 2019-11-29 09:10:57
Maybe it's just late, but I cannot figure out why this isn't working. When I have a post_save signal call a generic function, it works, but when I have a post_save signal call a method from a model, nothing happens. Here is code that works: class Revision(models.Model): # Model junk... def send_email(sender, instance, created, **kwargs): if created: print "DO STUFF" signals.post_save.connect(send_email, sender=Revision) But this does not work: class Revision(models.Model): # Model junk... def send_email(sender, instance, created, **kwargs): if created: print "DO STUFF" signals.post_save

Prevent delete in Django model

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 06:40:28
问题 I have a setup like this (simplified for this question): class Employee(models.Model): name = models.CharField(name, unique=True) class Project(models.Model): name = models.CharField(name, unique=True) employees = models.ManyToManyField(Employee) When an Employee is about to get deleted, I want to check whether or not he is connected to any projects. If so, deletion should be impossible. I know about signals and how to work them. I can connect to the pre_delete signal, and make it throw an