通过post方法限制访问api视图装饰器Django

时间:2016-05-26 10:55:12

标签: python django django-rest-framework restful-authentication

我创建了一个用户组,其名称为" Experts"我也安装了Django的rest_framework。我可以通过api访问我的项目和所有项目的评论。好吧,我应该通过api限制帖子操作到评论。这项行动将提供给专家'用户。我在views.py上有以下代码(我只是发布重要的代码):

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class MyCommand extends ContainerAwareCommand
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $em = $this->getContainer()->get('doctrine')->getEntityManager();
        // ...

我正在检查有关restful api的官方文档,但我不明白如何使用from .serializers import FeedbackSerializer class FeedbackViewSet(viewsets.ModelViewSet): """ API endpoint that allows Items to be viewed or edited. """ queryset = Feedback.objects.all().order_by('user') serializer_class = FeedbackSerializer def create(self, request, *args, **kwargs): self.permission_classes = ['IsAdminUser',] return super(FeedbackViewSet, self).create(self, request, *args, **kwargs) 覆盖通过api调用的方法来显示所有注释

1 个答案:

答案 0 :(得分:0)

class MyViewset(ModelViewSet):
    queryset = MyThing.objects.all()
    serializer_class = MyThingSerializer

    def update(self, request, pk=None, **kwargs):
        self.permission_classes = [CustomPermission,]

        return super(MyViewset, self).update(request, pk, **kwargs)