Modifies the as_sql methods to print the queries.
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 | from optparse import make_option
import os
import types
from django.core.management.commands.runserver import Command as BaseCommand
from django.db.models.sql import query
from django.db.models.sql import subqueries
class Command(BaseCommand):
help = "Works as runserver but prints every SQL statement used."
def handle(self, *args, **kwargs):
log_query(query.Query)
log_query(subqueries.DeleteQuery)
log_query(subqueries.InsertQuery)
log_query(subqueries.UpdateQuery)
super(Command, self).handle(*args, **kwargs)
def log_query(cls):
original_as_sql = cls.as_sql.im_func
def new_as_sql(*args, **kwargs):
q, args = original_as_sql(*args, **kwargs)
print q.replace('"', '') % args
print
return q, args
cls.as_sql = types.MethodType(new_as_sql, None, cls)
|
More like this
- Image compression before saving the new model / work with JPG, PNG by Schleidens 4 days, 13 hours ago
- Help text hyperlinks by sa2812 1 month ago
- Stuff by NixonDash 3 months ago
- Add custom fields to the built-in Group model by jmoppel 5 months, 1 week ago
- Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 8 months, 3 weeks ago
Comments
Please login first before commenting.