Rahul
Rahul

Reputation: 141

Django - How to Get current object in queryset?

Views.py

class ProfileView(UserPassesTestMixin, DetailView):
    template_name = "profile/profile_view.html"
    queryset = User.objects.all()
    context_object_name = 'profile'

def test_func(self):
    x = self.request.user.full_name
    print (x)
    y = ''
    if x == y:
        raise Http404("Profile Inactive")
    else:
        return True

Question - If the full_name of the user is empty i want to display Profile Inactive error but i am not able to print the full_name. I don't want to print the full_name of the logged in user but the user's page. the user's page is defined by slug localhost:8000/slug.

Upvotes: 2

Views: 4186

Answers (2)

Rahul
Rahul

Reputation: 141

x = self.get_object().full_name

Worked! thanks!

Upvotes: 0

Manan Mehta
Manan Mehta

Reputation: 5919

You can use self.get_object() to get the object based on the lookup field.

Upvotes: 2

Related Questions