0

I want to get user.pk from request (for logged in user) in order to avoid additional DB queries.

In views there is incoming request variable. How to get this request somewhere else (not in views)? Directly importing HttpRequest does not help, because HttpRequest gets user object because of middleware modification. How can I get this 'modified' HttpRequest with user object? What do I need to import?

EDIT: I want to implement customized user address, so user may have 2 types of addresses, like mysite.com/username or mysite.com/id123, so one link from navigation menu (My page) is dynamic. For the creation of navigation menu I use django-sitetree, where I want to do something like:

from sitetree.sitetreeapp import register_items_hook

def my_items_processor(tree_items, tree_sender):

    # get somehow `request`

    if tree_sender == 'menu.children':
        for item in tree_items:
            if request.user.username:
                item.url = request.user.username
            else:
                item.url = request.user.pk

    return tree_items
TitanFighter
  • 4,582
  • 3
  • 45
  • 73
  • Is your function still in the request-response cycle, i.e. is there a current request that you just don't have access to in the current scope? – knbk Aug 28 '16 at 14:41
  • @knbk, I added new info above. As I understand, my function is out of request-response cycle. I need somehow to get the model of current logged in user. Usually I can get it from `request`. I tried to find any other way how to `get current user`, but no luck. – TitanFighter Aug 28 '16 at 14:57
  • 1
    The concept of "currently logged in user" is directly tied to the request object. If this is outside the request-response cycle and no request exists, you can't access the logged in user because there isn't one. If it's in the request-response cycle you might be able to pass the request down to the function in some way, but if it isn't, then you're out of luck. – knbk Aug 28 '16 at 15:05
  • @knbk, thank you for the explanation. I will reorganize my code in order to put it in the request-responce cycle. If you want, you can make your comment as the answer and I accept it. – TitanFighter Aug 28 '16 at 15:15

1 Answers1

1

I just answered this same question (albeit relating to forms) in another question. Please see my answer there.

Assuming you have setup threadlocals.py as detailed in that post, and stored it as sitetree/threadlocals.py:

from sitetree.sitetreeapp import register_items_hook
from sitetree.threadlocals import get_current_user

def my_items_processor(tree_items, tree_sender):

    user = get_current_user()

    if tree_sender == 'menu.children':
        for item in tree_items:
            if user.username:
                item.url = user.username
            else:
                item.url = user.pk

    return tree_items
Community
  • 1
  • 1
Adam Hopkins
  • 6,837
  • 6
  • 32
  • 52
  • 1
    Despite the answer is correct, I realized that the approach I wanted to use is not correct :) Better way as I currently see is to add a `user_url` field to my models.py and store there whether user_id or username – TitanFighter Aug 28 '16 at 18:46
  • Good luck @TitanFighter! – Adam Hopkins Aug 28 '16 at 20:15