1👍
✅
You have a problem fetching the Project
instance by user’s id
value in get_object
method. You should call filter
first.
def get_object(self):
return Projects.objects.filter(user=self.kwargs['pk'])[0]
However, your problem is more fundamental. You should subclass ListView
not DetailView
to get the projects of a given user. For it to work for a url like /user/dhruv/, you can do the following;
urls.py
url(r'^user/(?P<author>\w+)/$', UserProjectsListView.as_view(), name='detail'),
views.py
class UserProjectsListView(ListView):
model = Projects
def get_queryset(self):
return self.model.objects.filter(author=self.kwargs['author'])
Hope this helps.
Source:stackexchange.com