[Fixed]-Django 1.10 query a table using username from logged in user

0👍

you can get the username of your logged in user by

username = request.user

you can simply pass your request parameter around to get all the information of the current session and do whatever query you wanna do.

👤Bobby

1👍

Remove the username = User.username line – User is the model class, not the current user instance.

You can access the current user if you set queryset, as this is loaded when the module is imported, not when the request is made. If you override the get_queryset method, you can access the user with self.request.user.

class RoosterListView(LoginRequiredMixin, ListView):
    def get_queryset(self):
        return Roosters.objects.filter(startvc__range=(DatumStart, DatumEind), username=self.request.user.username).order_by("startvc")[:35]

Leave a comment