﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28035	Allowing logging the queryset location for a given query	Rémy Hubscher	nobody	"The QuerySet creation and the QuerySet execution doesn't happens at the same time in Django.

However when you want to link a query in your database logs with your Python code, you want to see where the QuerySet have been created as well as were it was executed.

Each time you call a new method on your queryset it will create a new queryset with the new filter or order_by or annotate calls.

The plan would be to keep track of all these calls that generated a new QuerySet and to add it as a database comment when executing the request.

Then you would be able to link easily where your request have been generated from in your python code.
{{{
SELECT * FROM users WHERE id = 1; -- users/views.py L.21 `user = Users.objects.get(pk=request.user.id)`
}}}
It is especially useful when you create conditionals QuerySet:
{{{
SELECT * FROM users WHERE name LIKE 'enac%' ORDER BY username; -- users/views.py L.18 `qs = Users.objects.all() \n users/views.py L.25 `qs = qs.filter(name__startswith=""enac"")` \n users/views.py L.30 `qs = qs.order_by('username')` \n templates/user_search.html L.55 `{% for user in users %}`
}}}
If we want to do that as a library outside of Django, it means we would have to monkey patch the QuerySet object.
Do we want to make it a Django Feature?

We could add the comment only when `DEBUG = True` or `DATABASE_QUERY_LOGS = True`"	New feature	new	Database layer (models, ORM)	1.10	Normal				Accepted	0	0	0	0	0	0
