Django DetailView - 如何更改get_object以检查字段

Ale*_*nes 5 python django django-views django-class-based-views

所以我想做一个DetailView单独显示照片及其相关信息.但是,我想让它确保用户也有权访问照片.

这是视图的urls.py

url(r'^photo/(?P<slug>[\-\d\w]+)/$', views.PhotoDetail.as_view(), name='photo'),
Run Code Online (Sandbox Code Playgroud)

这是views.py

class PhotoDetail(DetailView):
    template_name = 'otologue/photo_detail.html'

    def get_object(self, queryset=None):
        slug = self.get_slug_field()
        print(slug)
        object_instance = Photo.objects.filter(slug=slug)
        print(object_instance)
        object_user = object_instance.photoextended.user
        user = get_object_or_404(User, username=self.request.user)  # Get the user in the view

        if object_user != user:  # See if the object_user is the same as the user
            return HttpResponseForbidden('Permission Error')

        else:
            return object_instance
Run Code Online (Sandbox Code Playgroud)

正如你所看到的那样我尝试get_slug_field()但是当我打印它时它只会说'slug'当slug应该说出url中的对象slug.从这里,object_instance没有任何对象,然后为什么我试图从中获取用户,它找不到任何数据.

Ala*_*air 19

DetailViewget_object方法已经知道如何用塞获取的对象.无需复制此代码,只需调用即可super().

然后你可以self.request.user直接比较用户- 没有必要从数据库中重新获取用户get_object_or_404.

最后,你无法返回响应get_object,该方法意味着返回对象.但是你可以提出一个例外Http404.

from django.http import Http404

class PhotoDetail(DetailView):

    def get_object(self, queryset=None):
        obj = super(PhotoDetail, self).get_object(queryset=queryset)
        if obj.user != obj.photoextended.user:
            raise Http404()
        return obj
Run Code Online (Sandbox Code Playgroud)

基于类的视图中的常见方法是get_queryset按用户覆盖和过滤.get_object获取对象时,该方法将使用此查询集.如果对象不在查询集中,则用户将收到404错误.

class PhotoDetail(DetailView):

    def get_queryset(self):
        queryset = super(PhotoDetail, self).get_queryset()
        return queryset.filter(photoextended__user=self.request.user)
Run Code Online (Sandbox Code Playgroud)