gPxl
gPxl

Reputation: 105

django-filter according to the logged user

I am using the django_filters to filtering the information. However, it is showing the data of all regitered user of my system. I need show only the data of logged user.

Follow the files:

filters.py

import django_filters
from apps.requisitos.models import Requisito

class RequisitoFilter(django_filters.FilterSet):
class Meta:
model = Requisito
fields = ['nomeRequisito', 'projeto']

views.py

class RequisitoList(ListView):
paginate_by = 10
model = Requisito

def get_queryset(self):
usuarioLogado = self.request.user
return Requisito.objects.filter(user=usuarioLogado)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['filter'] = RequisitoFilter(self.request.GET, queryset=self.queryset)
return context

the loop for of html page

{% for requisito in filter.qs %}

thank you very much

Upvotes: 1

Views: 459

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

You need to pass the result of get_queryset to the queryset parameter:

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['filter'] = RequisitoFilter(
        self.request.GET,
        queryset=self.get_queryset()
    )
    return context

Note: You might want to consider using a FilterView [GitHub], this view implements most of the ListView [Django-doc], and encapsulates logic to use a filter_class.

Upvotes: 1

Related Questions