django-signals

defining a custom post_migrate signal

落爺英雄遲暮 提交于 2019-11-27 16:23:24
问题 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

when to use pre_save, save, post_save in django?

六月ゝ 毕业季﹏ 提交于 2019-11-27 12:07:40
问题 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? 回答1: 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

How do I mock a django signal handler?

时光怂恿深爱的人放手 提交于 2019-11-27 11:53:54
问题 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

Extending django-registration using signals

北城以北 提交于 2019-11-27 11:23:48
问题 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

django - signals not working

試著忘記壹切 提交于 2019-11-27 10:47:31
问题 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)

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

自作多情 提交于 2019-11-27 10:46:34
问题 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

Django post_save preventing recursion without overriding model save()

断了今生、忘了曾经 提交于 2019-11-27 03:44:41
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 hooks isolates all fulfillment related code from our Order model. If we drop the fulfillment service,

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

£可爱£侵袭症+ 提交于 2019-11-27 03:16:48
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=company, name_first=user.first_name, name_last=user.last_name) profile = UserProfile.objects.create(user

Django: UserProfile with Unique Foreign Key in Django Admin

我们两清 提交于 2019-11-27 02:18:48
问题 I have extended Django's User Model using a custom user profile called UserExtension . It is related to User through a unique ForeignKey Relationship, which enables me to edit it in the admin in an inline form! I'm using a signal to create a new profile for every new user: def create_user_profile(sender, instance, created, **kwargs): if created: try: profile, created = UserExtension.objects.get_or_create(user=instance) except: pass post_save.connect(create_user_profile, sender=User) (as

Django manytomany signals? [duplicate]

荒凉一梦 提交于 2019-11-27 02:00:26
问题 This question already has an answer here: django manytomanyfield .add() method 2 answers Let's say I have such model class Event(models.Model) users_count = models.IntegerField(default=0) users = models.ManyToManyField(User) How would you recommend to update users_count value if Event add/delete some users ? 回答1: If possible in your case, you could introduce Participation model which would join Event and User: class Participation(models.Model): user = models.ForeignKey(User) event = models