django-signals

Identify the changed fields in django post_save signal

落爺英雄遲暮 提交于 2019-11-27 01:34:38
问题 I'm using django's post_save signal to execute some statements after saving the model. class Mode(models.Model): name = models.CharField(max_length=5) mode = models.BooleanField() from django.db.models.signals import post_save from django.dispatch import receiver @receiver(post_save, sender=Mode) def post_save(sender, instance, created, **kwargs): # do some stuff pass Now I want to execute a statement based on whether the value of the mode field has changed or not. @receiver(post_save, sender

Django post_save() signal implementation

情到浓时终转凉″ 提交于 2019-11-27 00:08:43
I have a question about django. I have ManyToMany Models here class Product(models.Model): name = models.CharField(max_length=255) price = models.DecimalField(default=0.0, max_digits=9, decimal_places=2) stock = models.IntegerField(default=0) def __unicode__(self): return self.name class Cart(models.Model): customer = models.ForeignKey(Customer) products = models.ManyToManyField(Product, through='TransactionDetail') t_date = models.DateField(default=datetime.now()) t_sum = models.FloatField(default=0.0) def __unicode__(self): return str(self.id) class TransactionDetail(models.Model): product =

Django accessing ManyToMany fields from post_save signal

£可爱£侵袭症+ 提交于 2019-11-26 22:32:05
I have a Django model and I want to modify the object permissions on or just after save. I have tried a few solutions and the post_save signal seemed the best candidate for what I want to do: class Project(models.Model): title = models.CharField(max_length=755, default='default') assigned_to = models.ManyToManyField( User, default=None, blank=True, null=True ) created_by = models.ForeignKey( User, related_name="%(app_label)s_%(class)s_related" ) @receiver(post_save, sender=Project) def assign_project_perms(sender, instance, **kwargs): print("instance title: "+str(instance.title)) print(

TransactionManagementError “You can't execute queries until the end of the 'atomic' block” while using signals, but only during Unit Testing

前提是你 提交于 2019-11-26 18:55:37
问题 I am getting TransactionManagementError when trying to save a Django User model instance and in its post_save signal, I'm saving some models that have the user as the foreign key. The context and error is pretty similar to this question django TransactionManagementError when using signals However, in this case, the error occurs only while unit testing . It works well in manual testing, but unit tests fails. Is there anything that I'm missing? Here are the code snippets: views.py @csrf_exempt

The right place to keep my signals.py file in a Django project

不想你离开。 提交于 2019-11-26 10:27:21
问题 Based on Django\'s documentation I was reading, it seems like signals.py in the app folder is a good place to start with, but the problem I\'m facing is that when I create signals for pre_save and I try to import the class from model it conflicts with the import in my model. # models.py from django.contrib.auth.models import User from django.db import models from django.utils.translation import gettext as _ from signals import * class Comm_Queue(CommunicatorAbstract): queue_statuses = ( (\'P\

Django post_save() signal implementation

﹥>﹥吖頭↗ 提交于 2019-11-26 09:19:41
问题 I have a question about django. I have ManyToMany Models here class Product(models.Model): name = models.CharField(max_length=255) price = models.DecimalField(default=0.0, max_digits=9, decimal_places=2) stock = models.IntegerField(default=0) def __unicode__(self): return self.name class Cart(models.Model): customer = models.ForeignKey(Customer) products = models.ManyToManyField(Product, through=\'TransactionDetail\') t_date = models.DateField(default=datetime.now()) t_sum = models.FloatField

Issue with ManyToMany Relationships not updating immediately after save

人走茶凉 提交于 2019-11-26 04:23:50
问题 I\'m having issues with ManytoMany Relationships that are not updating in a model when I save it (via the admin) and try to use the new value within a function attached to the post_save signal or within the save_model of the associated AdminModel . I\'ve tried to reload the object within those functions by using the get function with the id.. but it still has the old values. Is this a transaction issue? Is there a signal thrown when the transaction ends? Thanks, 回答1: When you save a model via