﻿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
23011	Getting user ID from a request shouldn't hit the database	Bertrand Bordage	nobody	"In a view, it is a common case to use the current user ID.
It is especially useful to limit the queryset to what the user authored.

To do so, one must use the `AuthenticationMiddleware` and write something like `qs.filter(author_id=request.user.pk)` in the view.

Unfortunately, request.user is a lazy object that generates a database hit when used.
This DB hit is useless since user ID is taken from the request object ([https://github.com/django/django/blob/fd23c06023a0585ee743c0752dc94da66694cf63/django/contrib/auth/__init__.py#L150 as shown here]).
There is a similar issue in templates when we use `context_processors.auth`.

I suggest we add a `user_id` attribute to the request in `AuthenticationMiddleware`.  That would be consistent with the foreign key idiom that ID can be directly accessed from `[fk_name]_id` without any extra DB hit.
I also suggest we add the same `user_id` to `context_processors.auth`."	New feature	closed	contrib.auth	dev	Normal	wontfix			Accepted	0	0	0	0	0	0
