[英]django-filter according to the logged user
I am using the django_filters to filtering the information.我正在使用 django_filters 来过滤信息。 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过滤器.py
import django_filters
from apps.requisitos.models import Requisito
class RequisitoFilter(django_filters.FilterSet):
class Meta:
model = Requisito
fields = ['nomeRequisito', 'projeto']
views.py视图.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 html页面的循环
{% for requisito in filter.qs %}
thank you very much非常感谢您
You need to pass the result of get_queryset
to the queryset
parameter:您需要将get_queryset
的结果get_queryset
给queryset
参数:
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 theListView
[Django-doc] , and encapsulates logic to use afilter_class
.注意:您可能需要考虑使用FilterView
[GitHub] ,该视图实现了大部分ListView
[Django-doc] ,并封装了使用filter_class
逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.