[Answer]-Django – session key of actuall logged user, not possible to get from request

1👍

Use pass user as keyword argument to your form. You do not need to jump through hoops and load active session key from request and then load user from decoded session. All you need to do is:

in view:

myform = MyFormClass(user= request.user)

in form:

class MyFormClass(forms.Form):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user', None)
        super(MyFormClass, self).__init__(*args, **kwargs)
        self.fields['categories'].queryset = Category.objects.filter(groups__in = self.user.groups.all())

NB! not complete working code. I just wanted to show you how you can use the self.user in queryset.

Leave a comment