Skip to content

Instantly share code, notes, and snippets.

@dchaplinsky
Created October 29, 2010 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dchaplinsky/653494 to your computer and use it in GitHub Desktop.
Save dchaplinsky/653494 to your computer and use it in GitHub Desktop.
Patch for django ORM to display all simple SELECT requests which makes lookup by one key
from django.db.models.query import QuerySet
old_get = QuerySet.get
def new_get(self, *args, **kwargs):
if len(kwargs) == 1:
for field_name in kwargs:
if "__" in field_name:
field_name, kind = field_name.split("__")
if kind != 'exact':
continue
if field_name == 'pk':
field = self.model._meta.pk
else:
field = self.model._meta.get_field(field_name)
if field.db_index or field.primary_key:
print "%sselect from %s by indexed field %s%s" % \
('\033[31m', self.model._meta.db_table, field_name, '\033[0m',)
return old_get(self, *args, **kwargs)
QuerySet.get = new_get
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment