different queryset based on permissions in Django Rest Framework

怎甘沉沦 提交于 2020-12-10 16:52:18

问题


I have seen this link, but I didn't find anything related to my question helping it being resolved.

Imagine we have to create a blog, in which posts have two status:

  1. is_draft
  2. published (published == !is_draft)

So, each user should see all of his/her posts, whether it is draft or not. In addition, Other users should see the published posts of rest of the users.

I am using viewsets in django and I know that we should have different queryset based on the current user permissions but I don't know how.

models.py:

from django.db import models

# Create your models here.
from apps.authors.models import Author


class Post(models.Model):
    author = models.ForeignKey(
        Author,
        related_name="posts",
        on_delete=models.CASCADE,
    )

    title = models.TextField(
        null=True,
        blank=True,
    )

    content = models.TextField(
        null=True,
        blank=True,
    )

    is_draft = models.BooleanField(
        default=True
    )

views.py:

from django.shortcuts import render
from rest_framework import viewsets, permissions
# Create your views here.
from apps.posts.models import Post
from apps.posts.serializers import PostSerializer


class PostViewSet(viewsets.ModelViewSet):
    queryset = Post.objects.all()
    serializer_class = PostSerializer

    def get_permissions(self):
        if self.action == "create":
            self.permission_classes = [permissions.IsAuthenticated]

        elif self.action == "list":
            pass #I don't know how can I change this part

        return super(PostViewSet, self).get_permissions()

serializers.py:

from rest_framework import serializers

from apps.posts.models import Post


class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = '__all__'


回答1:


Change your queryset like this in your viewset. That way, only your desired posts will be accessed/permitted by the view:

from django.shortcuts import render
from django.db.models import Q
from rest_framework import viewsets, permissions
# Create your views here.
from apps.posts.models import Post
from apps.posts.serializers import PostSerializer


class PostViewSet(viewsets.ModelViewSet):
    serializer_class = PostSerializer

    def get_permissions(self):
        if self.action == "create":
            self.permission_classes = [permissions.IsAuthenticated]

        return super(PostViewSet, self).get_permissions()

    def get_queryset(self, *args, **kwargs):
        current_user = self.request.user
        current_author = Author.objects.get(user=current_user) #assuming your author class has foreign key to user
        return Post.objects.filter(Q(author=current_author) | Q(is_draft=False))


来源:https://stackoverflow.com/questions/65156787/different-queryset-based-on-permissions-in-django-rest-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!