Login

django database snippet

Author:
ItsRLuo
Posted:
March 18, 2021
Language:
Python
Version:
3.0
Score:
1 (after 1 ratings)

..

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Found a django middleware snippet while looking at reducing db calls.
# -*- coding: utf-8 -*-

from __future__ import print_function
try:
    from django.utils.deprecation import MiddlewareMixin as Parent
except ImportError:
    Parent = object

import textwrap

from django.conf import settings
from pygments import highlight, lexers, formatters
from pygments_pprint_sql import SqlFilter


class SqlPrintMiddleware(Parent):
    """Output the sql used in the view.
    """
    def process_response(self, request, response):
        """Output all the sql queries for the Django orm.
        """
        from django.db import connection
        should_run = getattr(settings, 'SQLPRINT_MIDDLEWARE', True)
        if not should_run:
            return response
        x_db_hits = getattr(settings, 'X_DB_HITS', True)
        runnable = settings.DEBUG
        max_queries = getattr(settings, 'SQLPRINT_MAX_QUERIES', 1200)
        min_queries = getattr(settings, 'SQLPRINT_MIN_QUERIES', 0)

        queries = connection.queries
        dbhits = len(queries)
        if runnable and x_db_hits:
            response['X-DB-hits'] = str(dbhits)

        if runnable:
            if dbhits > min_queries:
                self.print_queries(request, queries)

            if max_queries and dbhits > max_queries:
                raise RuntimeError(textwrap.dedent("""\
                    A single request caused {dbhits} db hits, which is more than
                    settings.SQLPRINT_MAX_QUERIES
                    """.format(dbhits=dbhits)))

        return response

    def print_queries(self, request, queries):
        """Output all the queries.
        """
        lexer = lexers.MySqlLexer()
        lexer.add_filter(SqlFilter())

        totsecs = 0.0
        for query in queries:
            print(query['time'], 'used on:')
            totsecs += float(query['time'])
            print(highlight(query['sql'], lexer, formatters.TerminalFormatter()))

        print('Number of queries:', len(queries))
        print('Total time:', totsecs)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 4 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.