官方网站:
https://www.django-rest-framework.org/
翻译网站:
https://q1mi.github.io/Django-REST-framework-documentation/
RESTful 在Django框架中实现的插件:rest_framework
在Django框架中RESTful主要表现在序列化的实现、视图函数的实现和urls路由的实现,在这里我们可能会完全推翻Django基础知识中实现代码的方式。
1. 安装:
pip install restframework
2. 在Django中的配置:
1. settings.py中INSTALLED_APPS添加['rest-framework',]
2.在应用中创建Serializers.py
3.Serializers中三个基础的序列化类:
- serializers.HyperLinkedModelSerializer:序列化模型,并添加超链接
- serializers.Serializer:手动添加序列化
- serializers.ModelSerializer:自动序列化,只需要把模型导入,属性many=True 表示可序列化多个模型
3.1 。serializers.HyperLinkedModelSerializer:
# models.py
from django.db import models
class Books(models.Model):
b_name = models.CharField(max_length=30)
b_price = models.FloatField(default=1)
def to_dict(self):
return {'id':self.id, 'b_name':self.b_name, 'b_price':self.b_price}
# urls.py
from django.urls import include, path
from rest_framework import routers
from REST import views
router = routers.DefaultRouter()
router.register(r'Books', views.BookViewSet)
urlpatterns = [
path('', include(router.urls)),
]
from django.shortcuts import render
# Create your views here.
from rest_framework import viewsets
from REST.models import Books
from REST.serializers import BookSerializer
class BookViewSet(viewsets.ModelViewSet):
queryset = Books.objects.all()
serializer_class = BookSerializer
# serializers.py
from rest_framework import serializers
from REST.models import Books
class BookSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Books
fields = ('url', 'b_name','b_price')
models.py : 建立模型
urls.py :建立路由
views.py :建立序列化类视图
serializers.py :序列化
3.2 。serializers.Serializer
class Person(models.Model):
p_name = models.CharField(max_length=32)
p_age = models.IntegerField(default=1)
p_sex = models.BooleanField(default=False)
class Meta:
db_table = 'Person'
from django.urls import path
from REST import views
urlpatterns = [
path('person', views.PersonView.as_view()),
]
class PersonView(View):
def post(self,request):
p_name = request.POST.get('p_name')
p_age = request.POST.get('p_age')
print('*********',p_name)
person = Person()
person.p_name = p_name
person.p_age = p_age
print('***##***', p_name)
person.save()
person_serializer = PersonSerializer(person)
return JsonResponse(person_serializer.data)
class PersonSerializer(serializers.Serializer):
# 和模型中的字段对应
id = serializers.IntegerField(read_only=True) # 表示只读
p_name = serializers.CharField(max_length=32)
p_age = serializers.IntegerField(default=1)
p_sex = serializers.BooleanField(default=False)
# 进行对象的创建
def create(self,validated_data):
return Person.objects.create(**validated_data)
# 对象的更新
def update(self, instance, validated_data):
# 分别拿到对象,并对对象进行更新
instance.p_name = validated_data.get('p_name',instance.p_name)
print('@@@@@@')
instance.p_age = validated_data.get('p_age', instance.p_age)
instance.p_sex = validated_data.get('p_sex', instance.p_sex)
instance.save()
return instance
3.3 。serializers.ModelSerializer
class StudentSerializer(serializers.ModelSerializer):
class Meta:
model = Students
fields = ('s_name', 's_age')
(省略了models.py,urls.py,views.py,和serializers.Serializer类似)ModelSerializer简单,常用!!!
来源:oschina
链接:https://my.oschina.net/u/4365358/blog/4093321