码迷,mamicode.com
首页 > 其他好文 > 详细

REST_FRAMEWORK加深记忆-第二次练习官方文档

时间:2016-02-16 15:01:40      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

我想,其它几个基于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(rusers, views.UserViewSet)
router.register(rgroups, 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加深记忆-第二次练习官方文档

标签:

原文地址:http://www.cnblogs.com/aguncn/p/5192612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!