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

前端 未结 4 1795
清酒与你
清酒与你 2020-11-29 20:56

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

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 21:33

    This is an old question, but the solution I've found most straightforward is to use the 'raw' argument, passed by load data, and decorate the listener functions, for example:

    from functools import wraps
    
    
    def disable_for_loaddata(signal_handler):
        @wraps(signal_handler)
        def wrapper(*args, **kwargs):
            if kwargs['raw']:
                print "Skipping signal for %s %s" % (args, kwargs)
                return
            signal_handler(*args, **kwargs)
        return wrapper
    

    and then

    @disable_for_loaddata
    def callback_create_profile(sender, **kwargs):
        # check if we are creating a new User
        ...
    

提交回复
热议问题