MiniGunnR
MiniGunnR

Reputation: 5800

What is wrong with the custom method in DetailView

I am trying to compare the PK in the URL with the request.user.id so that no one can view other's profile. This might not be the conventional way to do it, but I'd still like to know what is wrong with my code. I'm a new learner so bear with me.

views.py

class UserDetail(DetailView):
    queryset = Profile.objects.all()
    template_name = 'details.html'

    def get_queryset(self):
        if self.request.user.id != self.kwargs['pk']:
            queryset = Profile.objects.first()
            return queryset
        else:
            return self.queryset

models.py

class Profile(AbstractUser):
    type = models.CharField(max_length=50)

urls.py

url(r'^details/(?P<pk>\d+)/$', login_required(views.UserDetail.as_view())),

When I go to the URL:

ERROR

Exception Type:     AttributeError
Exception Value:   'Profile' object has no attribute 'filter'

Upvotes: 0

Views: 45

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

A Profile instance is not a queryset.

You shouldn't be overriding get_queryset, you should be overriding get_object, which returns the specific object you want to display.

Upvotes: 1

Related Questions