[Django]-Django Class-based views: How to check an object value before returning view

3👍

I think your code is correct the way you have it, but there is no need to copy all Django’s get_object code.
Instead you can do it like so:

from django.views.generic.detail import SingleObjectMixin

class CheckObjectUserMixin(SingleObjectMixin):
    def get_object(self, queryset=None):
        obj = super(CheckObjectUserMixin, self).get_object(queryset)
        if obj.user_id != self.request.user.id:
            raise Http404
        return obj

Leave a comment