rest -方法

Posted forjie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rest -方法相关的知识,希望对你有一定的参考价值。

from django.contrib.auth.models import User

class UserSerializer(serializers.ModelSerializer):
snippets = serializers.PrimaryKeyRelatedField(many=True, queryset=Snippet.objects.all())

class Meta:
model = User
fields = (‘id‘, ‘username‘, ‘snippets‘)



对象级权限
from rest_framework import permissions


class IsOwnerOrReadOnly(permissions.BasePermission):
"""
Custom permission to only allow owners of an object to edit it.
"""

def has_object_permission(self, request, view, obj):
# Read permissions are allowed to any request,
# so we‘ll always allow GET, HEAD or OPTIONS requests.
if request.method in permissions.SAFE_METHODS:
return True

# Write permissions are only allowed to the owner of the snippet.
return obj.owner == request.user

以上是关于rest -方法的主要内容,如果未能解决你的问题,请参考以下文章

REST和RESTFUL

rest是啥意思

架构之:REST和RESTful

工具rest:Haskell的REST开源框架

REST与RESTful学习

REST与RESTful学习