MrDoc/app_api/serializers_app.py

100 lines
2.6 KiB
Python
Raw Normal View History

2020-05-16 21:27:00 +08:00
# coding:utf-8
# @文件: serializers_app.py
# @创建者:州的先生
# #日期2020/5/11
# 博客地址zmister.com
2020-08-30 13:51:26 +08:00
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer,SerializerMethodField
2020-05-16 21:27:00 +08:00
from app_doc.models import *
2021-05-28 22:28:41 +08:00
# 用户序列化器
class UserSerializer(ModelSerializer):
class Meta:
model = User
fields = (
'id', 'last_login', 'is_superuser', 'username', 'email', 'date_joined', 'is_active', 'first_name'
)
2020-05-16 21:27:00 +08:00
# 文集序列化器
class ProjectSerializer(ModelSerializer):
2020-08-30 13:51:26 +08:00
create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M')
2020-05-16 21:27:00 +08:00
class Meta:
model = Project
fields = ('__all__')
# 协作文集序列化器
class ProjectCollaSerializer(ModelSerializer):
project_id = serializers.SerializerMethodField(label="文集ID")
project_name = serializers.SerializerMethodField(label="文集名称")
username = serializers.SerializerMethodField(label='文集创建人')
top_doc = serializers.SerializerMethodField(label="上级")
class Meta:
model = ProjectCollaborator
fields = ('__all__')
def get_project_name(self,obj):
return obj.project.name
def get_project_id(self,obj):
return obj.project.id
def get_username(self,obj):
username = obj.project.create_user.username
return username
def get_top_doc(self,obj):
return 0
2020-05-16 21:27:00 +08:00
# 文档序列化器
class DocSerializer(ModelSerializer):
2020-08-30 13:51:26 +08:00
project_name = SerializerMethodField(label="所属文集")
modify_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
2020-05-16 21:27:00 +08:00
class Meta:
model = Doc
fields = ('__all__')
2020-08-30 13:51:26 +08:00
# 返回文档的所属文集
def get_project_name(self,obj):
pro_name = Project.objects.get(id=obj.top_doc).name
return pro_name
2020-05-16 21:27:00 +08:00
# 文档模板序列化器
class DocTempSerializer(ModelSerializer):
class Meta:
model = DocTemp
fields = ('__all__')
# 图片序列化器
class ImageSerializer(ModelSerializer):
username = serializers.SerializerMethodField(label="用户名")
2020-05-16 21:27:00 +08:00
class Meta:
model = Image
fields = ('__all__')
def get_username(self,obj):
return obj.user.username
2020-05-16 21:27:00 +08:00
# 图片分组序列化器
class ImageGroupSerializer(ModelSerializer):
class Meta:
model = ImageGroup
fields = ('__all__')
# 附件序列化器
class AttachmentSerializer(ModelSerializer):
username = serializers.SerializerMethodField(label="用户名")
2020-05-16 21:27:00 +08:00
class Meta:
model = Attachment
fields = ('__all__')
def get_username(self,obj):
return obj.user.username