#23011: Getting user ID from a request shouldn't hit the database
--------------------------------------+--------------------
     Reporter:  BertrandBordage       |      Owner:  nobody
         Type:  Cleanup/optimization  |     Status:  new
    Component:  contrib.auth          |    Version:  1.6
     Severity:  Normal                |   Keywords:
 Triage Stage:  Unreviewed            |  Has patch:  0
Easy pickings:  1                     |      UI/UX:  0
--------------------------------------+--------------------
 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`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/23011>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/058.f2a6ae5b19d80ca12035349aa6183592%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to