Reputation: 159
How do I get the current user related object using django's generic class DetailView
? Using function based views, I can obtain the object like this:
def foo(request):
friendProfile = get_object_or_404(request.user.profile.friends,username="admin")
What is the equivalent using detail view? I'm guessing it's something related to get_object
or get_context_data
but I can't fully understand the documents.
Thank you.
Upvotes: 1
Views: 965
Reputation: 165
request
is an attribute of class based views. To get the current user you should use self.request.user
.
On a DetailView
overload the get_queryset
to edit the queryset used to get the object.
I don't know precisely how your friend model is defined but let's assume it has a foreign key pointing to your profile with a related_name
set to friend_of
. Your view could be:
class FriendProfileDetail(DetailView):
model = Friend
def get_queryset(self):
return self.model.objects.filter(friend_of=self.request.user.profile)
Upvotes: 3