Issue
I am wondering how can I get the current user id in the views.py? Furthermore, how can I get the current user's group id? Do I have to directly query the database using the user id? Thank you very much.
Solution
View functions get request
as one of their parameters. Assuming you're using the built in auth app, the current user (if any) will be request.user
. This may be a representation of an anonymous user if the session does not have a logged-in user - see the docs if you don't want to allow that.
Users can be in many groups, so I'm not quite sure what you mean by "group id". The user's groups are available as a standard many-to-many field, as request.user.groups
. That's a manager object, so you can apply filters and such to it - to get them all, request.user.groups.all()
.
Answered By - Peter DeGlopper
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.