序列化组件:
知识点:Serializer(偏底层)、ModelSerializer(重点)、ListModelSerializer(辅助群改)
Serializer
序列化准备:
-
class User(models.Model): SEX_CHOICES = [ [0, '男'], [1, '女'], ] # 自定义的数据 name = models.CharField(max_length=64) pwd = models.CharField(max_length=32) phone = models.CharField(max_length=11, null=True, default=None) sex = models.IntegerField(choices=SEX_CHOICES, default=0) icon = models.ImageField(upload_to='icon', default='icon/default.jpg') class Meta: db_table = 'old_boy_user' #数据库表名 verbose_name = '用户' #表名中文 verbose_name_plural = verbose_name #表名祛除s def __str__(self): return '%s' % self.name
- 后台管理层:admin.py
-
# 在admin.py中进行注册 from django.contrib import admin from . import models admin.site.register(models.User)
- 配置层:settings.py
# 注册rest_framework INSTALLED_APPS = [ # ... 'rest_framework', ] # 配置数据库 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'day70', 'USER': 'root', 'PASSWORD': '123' } } # media资源 MEDIA_URL = '/media/' # 后期高级序列化类与视图类,会使用该配置 MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # media资源路径 # 国际化配置 LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False
主路由:项目下urls.py
urlpatterns = [ # ... url(r'^api/', include('api.urls')), url(r'^media/(?P.*) ', serve, {'document_root': settings.MEDIA_ROOT}), ]
子路由:应用下urls.py
urlpatterns = [ url(r'^users/$', views.User.as_view()), url(r'^users/(?P.*)/$ ', views.User.as_view()), ]
序列化使用
-
序列化层:api/serializers.py
"""
1)设置需要返回给前台 那些model类有对应的 字段,不需要返回的就不用设置了
2)设置方法字段,字段名可以随意,字段值有 get_字段名 提供,来完成一些需要处理在返回的数据
"""
# 序列化组件 - 为每一个model类通过一套序列化工具类 # 序列化组件的工作方式与django froms组件非常相似 from rest_framework import serializers, exceptions from django.conf import settings from . import models class UserSerializer(serializers.Serializer): name = serializers.CharField() phone = serializers.CharField() # 序列化提供给前台的字段个数由后台决定,可以少提供, # 但是提供的数据库对应的字段,名字一定要与数据库字段相同 # sex = serializers.IntegerField() # icon = serializers.ImageField() # 自定义序列化属性 # 属性名随意,值由固定的命名规范方法提供: # get_属性名(self, 参与序列化的model对象) # 返回值就是自定义序列化属性的值 gender = serializers.SerializerMethodField() def get_gender(self, obj): # choice类型的解释型值 get_字段_display() 来访问 return obj.get_sex_display() icon = serializers.SerializerMethodField() def get_icon(self, obj): # settings.MEDIA_URL: 自己配置的 /media/,给后面高级序列化与视图类准备的 # obj.icon不能直接作为数据返回,因为内容虽然是字符串,但是类型是ImageFieldFile类型 return '%s%s%s' % (r'http://127.0.0.1:8000', settings.MEDIA_URL, str(obj.icon))
视图层
""" 1)从数据库中将要序列化给前台的model对象,或是对个model对象查询出来 user_obj = models.User.objects.get(pk=pk) 或者 user_obj_list = models.User.objects.all() 2)将对象交给序列化处理,产生序列化对象,如果序列化的是多个数据,要设置many=True user_ser = serializers.UserSerializer(user_obj) 或者 user_ser = serializers.UserSerializer(user_obj_list, many=True) 3)序列化 对象.data 就是可以返回给前台的序列化数据 return Response({ 'status': 0, 'msg': 0, 'results': user_ser.data }) """
class User(APIView): def get(self, request, *args, **kwargs): pk = kwargs.get('pk') if pk: try: # 用户对象不能直接作为数据返回给前台 user_obj = models.User.objects.get(pk=pk) # 序列化一下用户对象 user_ser = serializers.UserSerializer(user_obj) # print(user_ser, type(user_ser)) return Response({ 'status': 0, 'msg': 0, 'results': user_ser.data }) except: return Response({ 'status': 2, 'msg': '用户不存在', }) else: # 用户对象列表(queryset)不能直接作为数据返回给前台 user_obj_list = models.User.objects.all() # 序列化一下用户对象 user_ser_data = serializers.UserSerializer(user_obj_list, many=True).data return Response({ 'status': 0, 'msg': 0, 'results': user_ser_data })
Serializer序列化模块 总结 序列化 ser: 1)设置需要返回给前台 那些model类有对应的 字段,不需要返回的就不用设置了 2)设置方法字段,字段名可以随意,字段值有 get_字段名 提供,来完成一些需要处理在返回的数据 view: 1)从数据库中将要序列化给前台的model对象,或是对个model对象查询出来 user_obj = models.User.objects.get(pk=pk) 或者 user_obj_list = models.User.objects.all() 2)将对象交给序列化处理,产生序列化对象,如果序列化的是多个数据,要设置many=True user_ser = serializers.UserSerializer(user_obj) 或者 user_ser = serializers.UserSerializer(user_obj_list, many=True) 3)序列化 对象.data 就是可以返回给前台的序列化数据 return Response({ 'status': 0, 'msg': 0, 'results': user_ser.data }) 反序列化 ser: 1)设置必填与选填序列化字段,设置校验规则 2)为需要额外校验的字段提供局部钩子函数,如果该字段不入库,且不参与全局钩子校验,可以将值取出校验 3)为有联合关系的字段们提供全局钩子函数,如果某些字段不入库,可以将值取出校验 4)重写create方法,完成校验通过的数据入库工作,得到新增的对象 view:
项目序列化准备:
INSTALLED_APPS = [ # ... 'rest_framework', ] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'dg_proj', 'USER': 'root', 'PASSWORD': '123', } } """ 任何__init__文件 import pymysql pymysql.install_as_MySQLdb() """ LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# 主 from django.conf.urls import url, include from django.contrib import admin from django.views.static import serve from django.conf import settings urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^api/', include('api.urls')), url(r'^media/(?P.*) ', serve, {'document_root': settings.MEDIA_ROOT}), ] # 子 from django.conf.urls import url from . import views urlpatterns = [ ]
多表设计
Book表:name、price、img、authors、publish、is_delete、create_time
Publish表:name、address、is_delete、create_time
Author表:name、age、is_delete、create_time
AuthorDetail表:mobile, author、is_delete、create_time
BaseModel基表
is_delete、create_time
上面四表继承基表,可以继承两个字段
基表
class BaseModel(models.Model): is_delete = models.BooleanField(default=False) create_time = models.DateTimeField(auto_now_add=True) # 设置 abstract = True 来声明基表,作为基表的Model不能在数据库中形成对应的表 class Meta: abstract = True
1、外键位置: 一对多 - 外键放多的一方 一对一 - 从逻辑正反向考虑,如作者表与作者详情表,作者删除级联删除详情,详情删除作者依旧存在,所以建议外键在详情表中 多对多 - 外键在关系表中 2、ORM正向方向连表查找: 正向:通过外键字段 eg: author_detial_obj.author 反向:通过related_name的值 eg:author_obj.detail 注:依赖代码见下方 3、连表操作关系: 1)作者删除,详情级联 - on_delete=models.CASCADE 2)作者删除,详情置空 - null=True, on_delete=models.SET_NULL 3)作者删除,详情重置 - default=0, on_delete=models.SET_DEFAULT 4)作者删除,详情不动 - on_delete=models.DO_NOTHING 注:拿作者与作者详情表举例 4、外键关联字段的参数 - 如何实现 断关联、目前表间操作关系、方向查询字段 i)作者详情表中的 author = models.OneToOneField( to='Author', related_name='detail', db_constraint=False, on_delete=models.CASCADE ) ii)图书表中的 publish = models.ForeignKey( to='Publish', related_name='books', db_constraint=False, on_delete=models.DO_NOTHING, ) authors = models.ManyToManyField( to='Author' related_name='books', db_constraint=False, ) 注:ManyToManyField不能设置on_delete,OneToOneField、ForeignKey必须设置on_delete(django1.x系统默认级联,但是django2.x必须手动明确)
from django.db import models # 图书管理系统:Book、Author、AuthorDetail、Publish """ Book表: name、price、img、authors、publish、is_delete、create_time Publish表: name、address、is_delete、create_time Author表: name、age、is_delete、create_time AuthorDetail表: mobile, author、is_delete、create_time """ # 1) 基表 class BaseModel(models.Model): is_delete = models.BooleanField(default=False) create_time = models.DateTimeField(auto_now_add=True) # 作为基表的Model不能在数据库中形成对应的表,设置 abstract = True class Meta: abstract = True class Book(BaseModel): """name、price、img、authors、publish、is_delete、create_time""" name = models.CharField(max_length=64) price = models.DecimalField(max_digits=5, decimal_places=2) img = models.ImageField(upload_to='img', default='img/default.jpg') publish = models.ForeignKey( to='Publish', db_constraint=False, # 断关联 related_name='books', # 反向查询字段:publish_obj.books 就能访问所有出版的书 on_delete=models.DO_NOTHING, # 设置连表操作关系 ) authors = models.ManyToManyField( to='Author', db_constraint=False, related_name='books' ) # 序列化插拔式属性 - 完成自定义字段名完成连表查询 @property def publish_name(self): return self.publish.name @property def author_list(self): return self.authors.values('name', 'age', 'detail__mobile').all() class Meta: db_table = 'book' verbose_name = '书籍' verbose_name_plural = verbose_name def __str__(self): return self.name class Publish(BaseModel): """name、address、is_delete、create_time""" name = models.CharField(max_length=64) address = models.CharField(max_length=64) class Meta: db_table = 'publish' verbose_name = '出版社' verbose_name_plural = verbose_name def __str__(self): return self.name class Author(BaseModel): """name、age、is_delete、create_time""" name = models.CharField(max_length=64) age = models.IntegerField() class Meta: db_table = 'author' verbose_name = '作者' verbose_name_plural = verbose_name def __str__(self): return self.name class AuthorDetail(BaseModel): """mobile, author、is_delete、create_time""" mobile = models.CharField(max_length=11) author = models.OneToOneField( to='Author', db_constraint=False, related_name='detail', on_delete=models.CASCADE, ) class Meta: db_table = 'author_detail' verbose_name = '作者详情' verbose_name_plural = verbose_name def __str__(self): return '%s的详情' % self.author.name
序列化
序列化层:api/serializers.py
from rest_framework.serializers import ModelSerializer, SerializerMethodField from rest_framework.exceptions import ValidationError from . import models # 可以单独作为Publish接口的序列化类,也可以作为Book序列化外键publish辅助的序列化组件 class PublishModelSerializer(ModelSerializer): class Meta: model = models.Publish fields = ('name', 'address') class BookModelSerializer(ModelSerializer): # 了解: 该方式设置的序列化字段,必须在fields中声明 # publish_address = SerializerMethodField() # def get_publish_address(self, obj): # return obj.publish.address # 自定义连表深度 - 子序列化方式 - 该方式不能参与反序列化,使用在序列化反序列化共存时,不能书写 publish = PublishModelSerializer() class Meta: # 序列化类关联的model类 model = models.Book # 参与序列化的字段 fields = ('name', 'price', 'img', 'author_list', 'publish') # 了解知识点 # 所有字段 # fields = '__all__' # 与fields不共存,exclude排除哪些字段 # exclude = ('id', 'is_delete', 'create_time') # 自动连表深度 # depth = 1
class Book(APIView): def get(self, request, *args, **kwargs): pk = kwargs.get('pk') if pk: try: book_obj = models.Book.objects.get(pk=pk, is_delete=False) book_data = serializers.BookModelSerializer(book_obj).data except: return Response({ 'status': 1, 'msg': '书籍不存在' }) else: book_query = models.Book.objects.filter(is_delete=False).all() book_data = serializers.BookModelSerializer(book_query, many=True).data return Response({ 'status': 0, 'msg': 'ok', 'results': book_data })
urlpatterns = [ url(r'^books/$', views.Book.as_view()), url(r'^books/(?P.*)/$ ', views.Book.as_view()), ]
反序列化
1 class BookModelDeserializer(ModelSerializer): 2 class Meta: 3 model = models.Book 4 fields = ('name', 'price', 'publish', 'authors') 5 # extra_kwargs 用来完成反序列化字段的 系统校验规则 6 extra_kwargs = { 7 'name': { 8 'required': True, 9 'min_length': 1, 10 'error_messages': { 11 'required': '必填项', 12 'min_length': '太短', 13 } 14 } 15 } 16 17 # 局部钩子 18 def validate_name(self, value): 19 # 书名不能包含 g 字符 20 if 'g' in value.lower(): 21 raise ValidationError('该g书不能出版') 22 return value 23 # 全局钩子 24 def validate(self, attrs): 25 publish = attrs.get('publish') 26 name = attrs.get('name') 27 if models.Book.objects.filter(name=name, publish=publish): 28 raise ValidationError({'book': '该书已存在'}) 29 return attrs 30 31 # ModelSerializer类已经帮我们实现了 create 与 update 方法
class Book(APIView): def post(self, request, *args, **kwargs): request_data = request.data book_ser = serializers.BookModelDeserializer(data=request_data) # raise_exception=True:当校验失败,马上终止当前视图方法,抛异常返回给前台 book_ser.is_valid(raise_exception=True) book_obj = book_ser.save() return Response({ 'status': 0, 'msg': 'ok', 'results': serializers.BookModelSerializer(book_obj).data })
urlpatterns = [ url(r'^books/$', views.Book.as_view()), url(r'^books/(?P.*)/$ ', views.Book.as_view()), ]
""" 1) fields中设置所有序列化与反序列化字段 2) extra_kwargs划分只序列化或只反序列化字段 write_only:只反序列化 read_only:只序列化 自定义字段默认只序列化(read_only) 3) 设置反序列化所需的 系统、局部钩子、全局钩子 等校验规则 """ class V2BookModelSerializer(ModelSerializer): class Meta: model = models.Book fields = ('name', 'price', 'img', 'author_list', 'publish_name', 'publish', 'authors') extra_kwargs = { 'name': { 'required': True, 'min_length': 1, 'error_messages': { 'required': '必填项', 'min_length': '太短', } }, 'publish': { 'write_only': True }, 'authors': { 'write_only': True }, 'img': { 'read_only': True, }, 'author_list': { 'read_only': True, }, 'publish_name': { 'read_only': True, } } def validate_name(self, value): # 书名不能包含 g 字符 if 'g' in value.lower(): raise ValidationError('该g书不能出版') return value def validate(self, attrs): publish = attrs.get('publish') name = attrs.get('name') if models.Book.objects.filter(name=name, publish=publish): raise ValidationError({'book': '该书已存在'}) return attrs
class V2Book(APIView): # 单查:有pk # 群查:无pk def get(self, request, *args, **kwargs): pk = kwargs.get('pk') if pk: try: book_obj = models.Book.objects.get(pk=pk, is_delete=False) book_data = serializers.V2BookModelSerializer(book_obj).data except: return Response({ 'status': 1, 'msg': '书籍不存在' }) else: book_query = models.Book.objects.filter(is_delete=False).all() book_data = serializers.V2BookModelSerializer(book_query, many=True).data return Response({ 'status': 0, 'msg': 'ok', 'results': book_data }) # 单增:传的数据是与model对应的字典 # 群增:传的数据是 装多个 model对应字典 的列表 def post(self, request, *args, **kwargs): request_data = request.data if isinstance(request_data, dict): many = False elif isinstance(request_data, list): many = True else: return Response({ 'status': 1, 'msg': '数据有误', }) book_ser = serializers.V2BookModelSerializer(data=request_data, many=many) # 当校验失败,马上终止当前视图方法,抛异常返回给前台 book_ser.is_valid(raise_exception=True) book_result = book_ser.save() return Response({ 'status': 0, 'msg': 'ok', 'results': serializers.V2BookModelSerializer(book_result, many=many).data }) # 单删:有pk # 群删:有pks | {"pks": [1, 2, 3]} def delete(self, request, *args, **kwargs): pk = kwargs.get('pk') if pk: pks = [pk] else: pks = request.data.get('pks') if models.Book.objects.filter(pk__in=pks, is_delete=False).update(is_delete=True): return Response({ 'status': 0, 'msg': '删除成功', }) return Response({ 'status': 1, 'msg': '删除失败', })
urlpatterns = [ url(r'^v2/books/$', views.V2Book.as_view()), url(r'^v2/books/(?P.*)/$ ', views.V2Book.as_view()), ]