Denis Denis -4 years ago 164
Python Question

How to intercept all read queries to DB in Django?

I need to intercept all queries to DB and write them to the file log.
So, if base operations like DELETE, UPDATE and CREATE I can intercept with signals framework. But what about READ queries? How can I catch them with signals?

Answer Source

Another way is to create custom middleware, and process queries in it

class SQLLoggingMiddleware(object):
    def process_request(self, request):
        from django.db import connection
        for query in connection.queries:
            #... do something with query
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download