6

I'm wondering whether it's possible to selectively suppress a Django signal (such as post_save or post_init) on object creation, or, alternatively, send it certain parameters.

What I have is a User object, which can be created in many different ways and places in my code. So to automatically assign a custom Profile object to each User, I use the post_save signal. However, in one specific case, there is extra information that I want to bind to the created Profile object. Passing it as arguments to the post_save signal would be great, but it doesn't look possible.

The other option is to manually create the Profile object, but then I need to that after the User is saved, otherwise the Profile cannot be bound to a User instance. Saving the User instance, however, results in another Profile being created via the function called by the signal.

And I can't just get the just-created Profile object, since that results in a 'Profile' object is unsubscriptable error. Any advice?

Update:

Here is an example of a possible situation:

def createUserProfile(sender, instance, created, **kwargs):
if created:  
    profile, created = Profile.objects.get_or_create(user=instance)
    if extra_param:
        profile.extra_param = extra_param
    profile.save()

post_save.connect(createUserProfile, sender=User)

def signup(request):
   ...
   extra_param = 'param'
   user.save()

How do I get the variable extra_param in the signup method to the createUserProfile method, where it is to be stored as part of the Profile object?

Herman Schaaf
  • 46,821
  • 21
  • 100
  • 139

2 Answers2

1

Why this doesn't work for you?

user = User(...)
user.save()
# profile has been created transparently by post_save event
profile = user.profile
profile.extra_stuff = '...'
profile.save()

If you are obsessed with parameters passing to event, possible but evil:

user = User()
user._evil_extra_args = { ... }
user.save()

In event:
extra_args = getattr(user, '_evil_extra_args', False)

It is evil because people who will read your code will have no idea what the heck those _evil_extra_args is for and how does it work.

Ski
  • 14,197
  • 3
  • 54
  • 64
  • 3
    I went with the evil option in the end, since the first one gives some sort of concurrency error (the db object is still locked from the `save()` event when I try to access the profile). – Herman Schaaf Feb 06 '11 at 12:21
0

Delaying post_save is not possible (unless you want to entirely disconnect it), but neither is it necessary. Passing a parameter to the profile is not a problem at all:

class UserProfile(models.Model):  
    user = models.ForeignKey(User)
    other = models.SomeField()

def create_user_profile(sender, instance, created, other_param=None, **kwargs):  
    if created:  
       profile, created = UserProfile.objects.get_or_create(user=instance)
       profile.other(other_param) # or whatever
       profile.save()

post_save.connect(create_user_profile, sender=User, other_param=p)
Community
  • 1
  • 1
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
  • What if I want to send that parameter only in one case, and the `post_save.connect` is defined somewhere other than the function creating the `User` object itself? How would that work? – Herman Schaaf Jan 27 '11 at 14:55
  • I think one of us is misunderstanding the other, so I added an example to my question that will hopefully clarify the problem. – Herman Schaaf Jan 27 '11 at 15:38