Queryset for current logged in user Django

前端 未结 4 1579
时光说笑
时光说笑 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:25

    There could be better ways to do this. As you are still learning below code does the trick.

    @login_required
    def search(request, *args, **kwargs):
        status_list = Data.objects.get(user=request.user)
    
        month = request.GET.get("month", None)
        year = request.GET.get("year", None)
    
        if month:
            status_list = status_list.filter(Date__month=month)
    
        if year:
            status_list = status_list.filter(Date__year=year)
    
        return render(request, 'users/data.html', {'filter': status_list})
    

提交回复
热议问题