django-signals

Django post_save preventing recursion without overriding model save()

老子叫甜甜 提交于 2019-12-17 07:14:45
问题 There are many Stack Overflow posts about recursion using the post_save signal, to which the comments and answers are overwhelmingly: "why not override save()" or a save that is only fired upon created == True . Well I believe there's a good case for not using save() - for example, I am adding a temporary application that handles order fulfillment data completely separate from our Order model. The rest of the framework is blissfully unaware of the fulfillment application and using post_save

How do I prevent fixtures from conflicting with django post_save signal code?

被刻印的时光 ゝ 提交于 2019-12-17 07:12:29
问题 In my application, I want to create entries in certain tables when a new user signs up. For instance, I want to create a userprofile which will then reference their company and some other records for them. I implemented this with a post_save signal: def callback_create_profile(sender, **kwargs): # check if we are creating a new User if kwargs.get('created', True): user = kwargs.get('instance') company = Company.objects.create(name="My Company") employee = Employee.objects.create(company

Django accessing ManyToMany fields from post_save signal

二次信任 提交于 2019-12-17 06:08:10
问题 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

Django signals vs. overriding save method

好久不见. 提交于 2019-12-17 03:55:32
问题 I'm having trouble wrapping my head around this. Right now I have some models that looks kind of like this: def Review(models.Model) ...fields... overall_score = models.FloatField(blank=True) def Score(models.Model) review = models.ForeignKey(Review) question = models.TextField() grade = models.IntegerField() A Review is has several "scores", the overall_score is the average of the scores. When a review or a score is saved, I need to recalculate the overall_score average. Right now I'm using

Django signals vs. overriding save method

瘦欲@ 提交于 2019-12-17 03:54:34
问题 I'm having trouble wrapping my head around this. Right now I have some models that looks kind of like this: def Review(models.Model) ...fields... overall_score = models.FloatField(blank=True) def Score(models.Model) review = models.ForeignKey(Review) question = models.TextField() grade = models.IntegerField() A Review is has several "scores", the overall_score is the average of the scores. When a review or a score is saved, I need to recalculate the overall_score average. Right now I'm using

Many to many field not shown in object while using signals to detect save operation in django

丶灬走出姿态 提交于 2019-12-13 13:17:16
问题 This is a follow up question to : Cant get post_save to work in Django My models are : class Car(models.Model): name = models.CharField(max_length=50) ... some other attributes of Car ... class Person(models.Model): car = models.ManyToManyField(Car) name = models.CharField(max_lenght=100) ... Some other attributes of Person ... class License(models.Model): person = models.ForeignKey(Person) ... Other attributes of License ... Signal handler: def signal_handler(sender, **kwargs): print 'Person

django-registration creating blank django-profiles using signals

試著忘記壹切 提交于 2019-12-13 07:36:12
问题 I have gone through the solution here and Shacker's django-profiles: The Missing Manual and subsequent signals solution by Dmitko and answers here and many places. Here are the details: AUTH_PROFILE_MODULE = "members.Profile " From members.models.py class Profile(models.Model): """member model.""" GENDER_CHOICES = ( (1, 'Male'), (2, 'Female'), ) user = models.ForeignKey(User, unique=True) first_name = models.CharField(_('first name'), max_length=100) middle_name = models.CharField(_('middle

Django pre_save signal

戏子无情 提交于 2019-12-13 02:23:37
问题 I needed to be able to change my model data before it's saved, so I considered using pre_save handler to be the best option: @receiver(pre_save, weak = False) def pre_category_save(sender, **kwargs): if kwargs['instance'].tags is None: kwargs['instance'].tags = kwargs['instance'].__unicode__().replace(' -> ', ', ') Under the instance key of kwargs I expected to find the actual model instance I'm saving, but instead I got an object of LogEntry class - that's the cause why my function fails

Working with Django's post_save() signal

倾然丶 夕夏残阳落幕 提交于 2019-12-12 09:44:20
问题 I have two tables: class Advertisement(models.Model): created_at = models.DateTimeField(auto_now_add=True) author_email = models.EmailField() class Verification(models.Model): advertisement = models.ForeignKeyField(Advertisement) key = models.CharField(max_length=32) And I need to auto populate Verification table after adding new advertisement. def gen_key(sender, instance, created, **kwargs): if created: from hashlib import md5 vkey = md5("%s%s" % (instance.author_email, instance.created_at)

Django signals with shopify webhooks

半腔热情 提交于 2019-12-12 02:59:38
问题 I am new to Django signals and Shopify webhooks, but I want to implement this feature in to a project. I am using this package, which also includes a set of WebhookSignals, to receive and verify the Shopify webhook, but then I want to do stuff with the information I receive (to be specific, I want to handle the customer information of an order and store it in a databse). I believe I need to use the provided signals to do this, but I don't really understand how to. So far, I've tried to put a