restful api

RESTful API 设计指南

理解RESTful架构

理解本真的REST架构风格

Python 之路,Restful API设计规范

python 之路,Django rest framework 初探

Django-rest-frameworl的使用

1.djangorestframework的安装

pip install djangorestframework
pip install markdown       # Markdown support for the browsable API.
pip install django-filter  # Filtering support

2.添加rest_framework到INSTALLED_APPS中

INSTALLED_APPS = (
    ...
    'rest_framework',
)

3.我们将创建一个API来访问我们项目的用户信息的读写。一个REST框架所有全局设置API是保存在一个配置为rest_framework字典。通过添加到settings.py模块,来使用

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ]
}

4.简单使用

(1)序列化器定义API,与model类相关联

rest_serializer.py

from repository import models
from rest_framework import serializers

# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = models.UserProfile
        fields = ('url', 'email', 'name', 'is_staff','is_active')

(2)视图集定义视图行为,是独立的rest_framework自定义视图,与数据表数据关联,需要register

rest_views.py

from Sale.rest_serializer import UserSerializer
from repository import models
from rest_framework import viewsets


# # ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
    queryset = models.UserProfile.objects.all()
    serializer_class = UserSerializer
#注意queryset和serializer是继承于父类的,我们不能修改

(3)在url文件中对视图集类进行注册

from django.conf.urls import url,include
from rest_framework import routers
from Sale.rest_views import UserViewSet

router = routers.DefaultRouter()
router.register(r'users', UserViewSet)

urlpatterns = [
    url(r'^api/', include(router.urls)),
]

5.开始访问

6.外键关联表的数据列显示

 

(1).需要再添加相关model的序列化类,视图集类,和url注册

rest_serializer.py添加

# Serializers define the API representation.
class RoleSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = models.Role
        fields = ('url', 'name')

rest_views.py添加

# # ViewSets define the view behavior.
class RoleViewSet(viewsets.ModelViewSet):
    queryset = models.Role.objects.all()
    serializer_class = RoleSerializer

urls中注册

from Sale.rest_views import UserViewSet,RoleViewSet

router.register(r'roles', RoleViewSet)

注意:此时也可以对role表添加和修改

(2)修改数据展示方法

修改序列化类即可

from repository import models
from rest_framework import serializers

# Serializers define the API representation.
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserProfile
        fields = ('url', 'email', 'name', 'is_staff','is_active','role')

# Serializers define the API representation.
class RoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = ('name',)
rest_serializer.py

7.显示多层数据

 

修改序列化类,添加depth

from repository import models
from rest_framework import serializers

# Serializers define the API representation.
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserProfile
        depth = 3
        fields = ('url', 'email', 'name', 'is_staff','is_active','role')

# Serializers define the API representation.
class RoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = ('name',)

注意:

添加depth后,会对每个设置显示的字段进行展开,而且会将其全部字段显示

8.在Django中view视图中使用,进行添加

from Sale import rest_serializer

def api_test(request):
    serilizer_obj = None
    if request.method == "POST":
        data = json.loads(request.POST.get("data"))

        serilizer_obj = rest_serializer.UserSerializer(data=data)
        if serilizer_obj.is_valid():
            serilizer_obj.save()

    return render(request,"sale/api-test.html",{'serilizer_obj':serilizer_obj})

可以正常进行添加操作

 

posted @ 2018-06-16 23:26  山上有风景  阅读(289)  评论(0编辑  收藏  举报