开发者

Is it possible to selectively suppress a post_save (or other) signal in Django?

开发者 https://www.devze.com 2023-02-07 05:39 出处:网络
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.

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 a开发者_运维百科ssign 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?


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.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消