[Fixed]-Detect change in apps and user who made changes by a separate app Django.

1đź‘Ť

âś…

You can add a primitive middleware into your “activitylogapp” with process_request to store the request.

As for storing the request, I see 2 options:

  1. Ugly but fast to implement. Save the request globally. It shouldn’t affect anything as you get a fresh new cloned thread for each request, which dies right after the request has been processed.

  2. More sophisticated, without globals. Utilize the fact, that Django signals create weak references to their receiver functions. So you can attach save_handler to the request itself, and they will get GC-ed together in the end. Something like that:

    class MyMiddleware(object):
    
        def process_request(request):
    
            def save_handler(sender, instance, created, **kswargs):
                user = request.user
                "do the stuff you want to do"
    
            # without the following line, the save_handler will be
            # garbage collected right away since nothing references it
            request._my_save_handler_instance = save_handler 
    
            post_save.register(save_handler, ...)
    
👤Art

Leave a comment