question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

current_user instance is of LocalProxy type

See original GitHub issue

Hi, when using current_user instance I was having a weird bug with MongoEngine, then I decided to debug and when checking:

ipdb> type(current_user)
<class 'werkzeug.local.LocalProxy'>

That’s not desired if I will use it directly. MongoEngine complains when using that instance during query/update, etc… Is there any way to unwrap the proxy? Right now I have to query to the DB to get the real object.

Thanks

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:35 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
dendikcommented, Apr 23, 2014

pahaz, the good workaround in my case was to add this mixin to my User class:

class TrueSelf(object):

    @property
    def self(self):
        """If self was pointed to by a proxy, present the true self.

        E.g. if self was returned by current_user, which is a proxy,
        we actually want to have the actual User object on an entry,
        not the one varying with each request.

        Also vital for storing into DB.
        """
        return self

class User(..., TrueSelf):
    ...

And then I could store current_user.self (current_user itself is still the proxy!) in the database. Or, in your case, it would be g.user = current_user.self (you have to test it; I don’t remember if current_user already takes the proper value in before_request handler).

I support your request to reopen the ticket!

0reactions
maxcountrymancommented, Jun 15, 2016

@asteinlein there’s nothing stopping you from implementing that yourself. I’m locking this topic now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Flask-Login for User Management with Flask
I don't think your logout function will work. Per the Flask-Login docs, the current_user object is a proxy of type <class 'werkzeug.local.localproxy'="">.
Read more >
firebase - currentUser() is not working type why I am seeing ...
Why do you invoke currentUser as a function ? It is a field, not a method. final user = await _auth.currentUser;. Update: Since...
Read more >
API Reference — Flask-Discord 0.1.64 documentation
It uses Flask session local proxy object to save state, authorization token and ... Permissions representing permissions of current user in the guild....
Read more >
Context Locals — Werkzeug Documentation (2.1.x)
Use issubclass(type(x), LocalProxy) to check if an object is a proxy. repr(user) # <User admin> isinstance( ...
Read more >
flask_login — Flask-Login 0.3.2 documentation
If no user is logged in, this will be an #: anonymous user current_user = LocalProxy(lambda: _get_user()) #: The default name of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found