Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:37:57+00:00 2026-06-18T12:37:57+00:00

I extended the Django CursorDebugWrapper execute routine so on the server side I can

  • 0

I extended the Django CursorDebugWrapper execute routine so on the server side I can keep track of all SQL executed. Looks like this:

class PrintQueryWrapper(django.db.backends.util.CursorDebugWrapper):
    def execute(self, sql, params=()):
        try:
            return self.cursor.execute(sql, params)
        finally:
            # record sql and do other stuff

django.db.backends.util.CursorDebugWrapper = PrintQueryWrapper

The problem is that I need each user session to do this individually, otherwise it will record all sql from all users with no concept of what came from where. If I can access the user ID from execute() I can easily keep track of the SQL from each user, but I don’t know how to do that without having access to the request object.

Note: I cannot claim credit for the above code. It originated from the Django Debug Toolbar.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:37:58+00:00Added an answer on June 18, 2026 at 12:37 pm

    You can make it with middleware:

    middleware.py:

    import django.db.backends.util
    
    
    class PrintQueryWrapper(django.db.backends.util.CursorDebugWrapper):
        user_id = None
        def execute(self, sql, params=()):
            try:
                return self.cursor.execute(sql, params)
            finally:
                print self.user_id, ": ", sql   
    
    class PrintQueryMiddleware(object):
        def process_request(self, request): 
            PrintQueryWrapper.user_id = request.user.pk
            django.db.backends.util.CursorDebugWrapper = PrintQueryWrapper
    

    settings.py:

    MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'main.middleware.PrintQueryMiddleware',         # chnage path to your app
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently extended with UserProfile so my admin.py looks like this: from django.contrib import
I've extended Django's default user class like this: class CustomUser(User): friends = models.ManyToManyField('self', symmetrical=False)
I've extended Django's contrib.auth.models user like: class UserProfile(models.Model): user = models.ForeignKey(User, related_name = 'profile')
I extended the User model in django to include several other variables, such as
I extended the JAAS javax.security.auth.spi.LoginModule, and installed it into a WAS server. It works;
I have extended Django's User Model using a custom user profile called UserExtension .
In my application I have an extended User model called UserProfile. This user can
Synopsis: Each User account has a UserProfile to hold extended info like phone numbers,
I have an extended UserProfile model in django: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True)
I implemented django-social-auth and got it to authenticate me with Facebook. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.