Queryset for current logged in user Django

前端 未结 4 1583
时光说笑
时光说笑 2021-01-14 06:22

I am doing queryset with my model. Now the queryset is displaying all the data in my html page. But I want to display only the logged in users data.

models.p

4条回答
  •  无人共我
    2021-01-14 07:16

    status_list = Data.objects.get(user=self.request.user) 
    

    is wrong as self is only used in class based views. No wonder it did not work. Please try with the below code instead

    status_list = Data.objects.filter(user=request.user) or 
    status_list = Data.objects.get(user=request.user)
    

    So, the final code will be like

    @login_required
    def search(request):
        status_list = Data.objects.get(user=request.user)
        render(request, 'users/data.html', {'filter': status_list})
    

提交回复
热议问题