django-signals

defining a custom post_migrate signal

一个人想着一个人 提交于 2019-11-29 02:06:41
I'm doing some kind of refactoring for my project, where I'm relying on the django django.contrib.auth.models.Permission model. So far I define the permissions for each new user using a post_save signal, so when the user is created, I assign their permissions using user.user_permissions.add(the_permission) , this works perfectly. Now I want to use the django.contrib.auth.models.Group model to clasify the permissions a user should have. This is my code: from django.apps import AppConfig from django.db.models.signals import post_migrate from django.contrib.auth.models import Group, Permission

How to use django-notification to inform a user when somebody comments on their post

好久不见. 提交于 2019-11-28 19:43:08
I have been developing in django for sometime now, and have developed a neat website having functionality such as writing blogs, posting questions, sharing content etc. However there is still one thing that is missing and i.e. creating notification for users. What I want to do is to inform users in their profiles, whenever somebody comments on their posts, or if they are following a particular post and there is an update on it, then inform the user of that update. I have looked around many applications but I am still very confused about how to do it. In case of using django-notification I seem

when to use pre_save, save, post_save in django?

安稳与你 提交于 2019-11-28 19:16:26
I see I can override or define pre_save, save, post_save to do what I want when a model instance gets saved. Which one is preferred in which situation and why? I shall try my best to explain it with an example: pre_save and post_save are signals that are sent by the model. In simpler words, actions to take before or after the model's save is called. A save triggers the following steps Emit a pre-save signal. Pre-process the data. Most fields do no pre-processing — the field data is kept as-is. Prepare the data for the database. Insert the data into the database. Emit a post-save signal. Django

How do I mock a django signal handler?

人走茶凉 提交于 2019-11-28 19:07:05
I have a signal_handler connected through a decorator, something like this very simple one: @receiver(post_save, sender=User, dispatch_uid='myfile.signal_handler_post_save_user') def signal_handler_post_save_user(sender, *args, **kwargs): # do stuff What I want to do is to mock it with the mock library http://www.voidspace.org.uk/python/mock/ in a test, to check how many times django calls it. My code at the moment is something like: def test_cache(): with mock.patch('myapp.myfile.signal_handler_post_save_user') as mocked_handler: # do stuff that will call the post_save of User self.assert

Extending django-registration using signals

喜夏-厌秋 提交于 2019-11-28 18:28:52
I have found here on stackoverflow a solution to extend django-registration with new fields using signals. Here's the link : http://dmitko.ru/?p=546 . I have created extended profile model, extended form, added required options to settings , defined urls and the proper form is displayed but only normal User (from auth module) is created. Why is that happening ? account.models : from django.db import models from django.contrib.auth.models import User from registration.signals import user_registered import hashlib class InheritedProfile(models.Model): first_name = models.CharField("Name", max

Django: How to access original (unmodified) instance in post_save signal

六眼飞鱼酱① 提交于 2019-11-28 18:13:12
I want to do a data denormalization for better performance, and put a sum of votes my blog post receives inside Post model: class Post(models.Model): """ Blog entry """ author = models.ForeignKey(User) title = models.CharField(max_length=255) text = models.TextField() rating = models.IntegerField(default=0) # here is the sum of votes! class Vote(models.Model): """ Vote for blog entry """ post = models.ForeignKey(Post) voter = models.ForeignKey(User) value = models.IntegerField() Ofcourse, I need to keep Post.rating value actual. Nornally I would use database triggers for that, but now I've

django - comparing old and new field value before saving

房东的猫 提交于 2019-11-28 18:07:20
I have a django model, and I need to compare old and new values of field BEFORE saving. I've tried the save() inheritence, and pre_save signal. It was triggered correctly, but I can't find the list of actualy changed fields and can't compare old and new values. There is a way? I need it for optimization of presave actions. Thank you! There is very simple django way for doing it. "Memorise" the values in model init like this: def __init__(self, *args, **kwargs): super(MyClass, self).__init__(*args, **kwargs) self.initial_parametername = self.parametername --- self.initial_parameternameX = self

django - signals not working

限于喜欢 提交于 2019-11-28 17:50:37
I am trying to create activity streams of users from their status. models: class Status(models.Model): body = models.TextField(max_length=200) image = models.ImageField(blank=True, null=True, upload_to=get_upload_file_name) privacy = models.CharField(max_length=1,choices=PRIVACY, default='F') pub_date = models.DateTimeField(auto_now_add=True, auto_now=False) user = models.ForeignKey(User) class Activity(models.Model): actor = models.ForeignKey(User) action = models.CharField(max_length=100) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object =

What are the options for overriding Django's cascading delete behaviour?

大城市里の小女人 提交于 2019-11-28 16:57:27
Django models generally handle the ON DELETE CASCADE behaviour quite adequately (in a way that works on databases that don't support it natively.) However, I'm struggling to discover what is the best way to override this behaviour where it is not appropriate, in the following scenarios for example: ON DELETE RESTRICT (i.e. prevent deleting an object if it has child records) ON DELETE SET NULL (i.e. don't delete a child record, but set it's parent key to NULL instead to break the relationship) Update other related data when a record is deleted (e.g. deleting an uploaded image file) The

How to use Django model inheritance with signals?

本秂侑毒 提交于 2019-11-28 16:26:59
问题 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 =