Python 如何将django rest框架设置为只允许创建对象的用户获取、放置、删除
我正在建立一个Django REST API,它涉及家庭、父母和孩子。我想设置权限,以便管理员可以向/api/parents/endpoint发送GET请求,并查看所有父对象的列表,但用户的GET请求只返回他们创建的父对象列表,或“拥有的” 我正在构建一个带有React.js前端和Django REST后端的应用程序。当用户注册时,他们将使用django拥有的开源simplejwt包获得JSON Web令牌。申请涉及家庭中的父母和子女。当用户尝试创建父对象时,他们需要能够检索到其个人创建的父对象的唯一超链接URL,以便为其子对象创建链接的子对象并仅将其与父对象关联 现在,当我向父路由器的端点发出POST请求时,只要登录,任何人都可以创建父对象。只有来自管理员的GET请求成功(请求中附加的JWT作为授权的承载)。Admin GET请求通过数据库中所有父对象的列表成功。如果创建父对象的用户使用其个人JWT发送GET请求,则返回空列表 我想实现权限,以便管理员可以获取、发布、放置和删除。除此之外,只有使用JWT进行身份验证并创建(和“拥有”)和对象的用户才能查看它 例如,如果我以user:newuser01的身份创建了一个帐户,并创建了3个父对象,那么对端点/api/parents/的GET请求应该返回我创建的3个父对象的JSON或“own” 如果数据库中总共有10个父对象,则管理员对同一端点的GET请求应返回所有10个 我所尝试的:Python 如何将django rest框架设置为只允许创建对象的用户获取、放置、删除,python,django,Python,Django,我正在建立一个Django REST API,它涉及家庭、父母和孩子。我想设置权限,以便管理员可以向/api/parents/endpoint发送GET请求,并查看所有父对象的列表,但用户的GET请求只返回他们创建的父对象列表,或“拥有的” 我正在构建一个带有React.js前端和Django REST后端的应用程序。当用户注册时,他们将使用django拥有的开源simplejwt包获得JSON Web令牌。申请涉及家庭中的父母和子女。当用户尝试创建父对象时,他们需要能够检索到其个人创建的父对象
- 在settings.py(ref:)中将DjangObjectPermissions实现为默认权限
- 使用django restframework guardian作为对象权限后端,并遵循其设置对象级权限的基本示例(参考:)
- 按照《卫报》的建议创建CustomObjectsPermissions
- 创建我自己的自定义权限,如下所示:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'api',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'rest_auth.registration',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'corsheaders',
'phonenumber_field',
'guardian',
]
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', # this is default
'guardian.backends.ObjectPermissionBackend',
)
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.DjangoObjectPermissions',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.SessionAuthentication',
#'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
}
class Parent(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
def __str__(self):
return "{}".format(self.first_name + ' ' + self.last_name)
class ParentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Parent
fields = ('url', 'first_name', 'last_name')
class ParentViewSet(viewsets.ModelViewSet):
queryset = Parent.objects.all()
serializer_class = ParentSerializer
permission_classes=[CustomObjectsPermissions]
来自models.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'api',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'rest_auth.registration',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'corsheaders',
'phonenumber_field',
'guardian',
]
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', # this is default
'guardian.backends.ObjectPermissionBackend',
)
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.DjangoObjectPermissions',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.SessionAuthentication',
#'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
}
class Parent(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
def __str__(self):
return "{}".format(self.first_name + ' ' + self.last_name)
class ParentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Parent
fields = ('url', 'first_name', 'last_name')
class ParentViewSet(viewsets.ModelViewSet):
queryset = Parent.objects.all()
serializer_class = ParentSerializer
permission_classes=[CustomObjectsPermissions]
来自序列化程序。py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'api',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'rest_auth.registration',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'corsheaders',
'phonenumber_field',
'guardian',
]
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', # this is default
'guardian.backends.ObjectPermissionBackend',
)
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.DjangoObjectPermissions',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.SessionAuthentication',
#'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
}
class Parent(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
def __str__(self):
return "{}".format(self.first_name + ' ' + self.last_name)
class ParentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Parent
fields = ('url', 'first_name', 'last_name')
class ParentViewSet(viewsets.ModelViewSet):
queryset = Parent.objects.all()
serializer_class = ParentSerializer
permission_classes=[CustomObjectsPermissions]
来自视图。py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'api',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'rest_auth.registration',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'corsheaders',
'phonenumber_field',
'guardian',
]
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', # this is default
'guardian.backends.ObjectPermissionBackend',
)
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.DjangoObjectPermissions',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.SessionAuthentication',
#'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
}
class Parent(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
def __str__(self):
return "{}".format(self.first_name + ' ' + self.last_name)
class ParentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Parent
fields = ('url', 'first_name', 'last_name')
class ParentViewSet(viewsets.ModelViewSet):
queryset = Parent.objects.all()
serializer_class = ParentSerializer
permission_classes=[CustomObjectsPermissions]
来自permissions.py(我尝试过的两个选项都包括):
期望值:新用户向/api/parents/发送帖子,并创建父对象。然后,用户可以发送一个GET to/api/parents/并只查看他们创建的父级的列表。管理员可以发送GET to/api/parents/并查看任何用户创建的所有父级
截至目前的结果:
或者,用户不允许做任何事情,但是管理员可以做任何事情
或者,用户可以创建父对象,但用户在发送GET请求时只能看到空列表