“Django RESTful api”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
第83行: 第83行:
 
         model=Group
 
         model=Group
 
         fields=('url','name')
 
         fields=('url','name')
 +
 +
 +
 +
#视图  on api/views.py
 +
from django.shortcuts import render
 +
from django.contrib.auth.models import User,Group
 +
from rest_framework import viewsets
 +
from api.serializers import UserSerializer,GroupSerializer
 +
 +
# Create your views here.
 +
class UserViewSet(viewsets.ModelViewSet):
 +
    queryset = User.objects.all()
 +
    serializer_class = UserSerializer
 +
 +
class GroupViewSet(viewsets.ModelViewSet):
 +
    queryset = Group.objects.all()
 +
    serializer_class = GroupSerializer
 +
# Create your views here.
 +
 +
 +
 +
 
</pre>
 
</pre>
  

2021年6月7日 (一) 06:45的版本

知识点

 RESTful API现在很流行,这里是它的介绍 理解RESTful架构RESTful API设计指南. 按照Django的常规方法当然也可以实现REST,但有一种更快捷、强大的方法,那就是 Django REST framework.它是python的一个模块,通过在Django里面配置就可以把app的models中的各个表实现RESTful API

过程

install

pip3 install djangorestframework #Django REST Framework
pip3 install markdown       # Markdown support for the browsable API.
pip3 install django-filter  # Filtering support

创建API

python3  manage.py  startapp api #当然 得先创建项目 



setting.py  添加 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'api',

]


REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    )
}


 数据库迁移
  python3 manage.py makemigrations
 python3 manage.py migrate



用我以前创建的可以用 

创建数据
点击主页右上角的Log in登录超级管理员账户,然后分别创建Group和User数据


创建用户和组在这里 

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "users": "http://127.0.0.1:8000/users/?format=api",
    "groups": "http://127.0.0.1:8000/groups/?format=api"
}

 

files

# on api/serializers.py

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')



#视图  on api/views.py
from django.shortcuts import render
from django.contrib.auth.models import User,Group
from rest_framework import viewsets
from api.serializers import UserSerializer,GroupSerializer

# Create your views here.
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

class GroupViewSet(viewsets.ModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
# Create your views here.




troubleshooting

应该是另外一个例子

这种接口可能和得少 不过学习还是有用的 note

  1. on urls.py

from django.urls import path, include

那个views_api 我放到views on apps(django_web)

Django 单元测试笔记

Django Web接口开发

see also

good Django Restful接口开发与测试

利用Django实现RESTful API(一)