4👍
✅
See the docs on dynamic filtering.
You should subclass ListView
and override the get_queryset
method. When the class-based view is called, self
is populated with the current request (self.request
) as well as anything captured from the URL (self.args
, self.kwargs
).
from django.views.generic import ListView
from myapp.models import Record
class MyRecordsListView(ListView):
context_object_name = 'record_list'
template_name = 'records.html',
def get_queryset(self):
return Record.objects.filter(user=self.request.user)
There’s also documentation for decorating class-based views. Basically you can just decorate the result of as_view
in your urls.py
:
(r'^myrecords/$', login_required(MyRecordsListView.as_view())),
Source:stackexchange.com