REST_FRAMEWORK加深记忆-第二次练习官方文档
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了REST_FRAMEWORK加深记忆-第二次练习官方文档相关的知识,希望对你有一定的参考价值。
我想,其它几个基于PYTHON的REST API模块概念都差不多吧。
先深入搞定这个吧。
前几次练习完了有一些印象,并且在工作中实践过一个,现在多弄几次,玩熟悉点。
Serializers.py
__author__ = ‘CHENGANG882‘ from django.contrib.auth.models import User, Group from rest_framework import serializers class UserSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = User fields = (‘url‘, ‘username‘, ‘email‘, ‘groups‘) class GroupSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Group fields = (‘url‘, ‘name‘)
VIEWS.PY
from django.contrib.auth.models import User, Group from rest_framework import viewsets from quickstart.serializers import UserSerializer, GroupSerializer class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all().order_by(‘-date_joined‘) serializer_class = UserSerializer class GroupViewSet(viewsets.ModelViewSet): queryset = Group.objects.all() serializer_class = GroupSerializer
URLS.PY
from django.conf.urls import include, url from django.contrib import admin from rest_framework import routers from quickstart import views router = routers.DefaultRouter() router.register(r‘users‘, views.UserViewSet) router.register(r‘groups‘, views.GroupViewSet) urlpatterns = [ url(r‘^admin/‘, include(admin.site.urls)), url(r‘^‘, include(router.urls)), url(r‘^api-auth/‘, include(‘rest_framework.urls‘, namespace=‘rest_framework‘)) ]
以上是关于REST_FRAMEWORK加深记忆-第二次练习官方文档的主要内容,如果未能解决你的问题,请参考以下文章
REST_FRAMEWORK加深记忆-加了用户登陆认证,自定义权限的API接口