From 5d880a7822a57a9be033a870d59d43ded8394189 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Sat, 15 May 2021 09:14:10 +0900 Subject: [PATCH] Refactor project serializers --- backend/api/serializers.py | 53 ++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/backend/api/serializers.py b/backend/api/serializers.py index ff591da6..1be0b142 100644 --- a/backend/api/serializers.py +++ b/backend/api/serializers.py @@ -140,61 +140,64 @@ class ProjectSerializer(serializers.ModelSerializer): class Meta: model = Project - fields = ('id', 'name', 'description', 'guideline', 'users', 'current_users_role', 'project_type', - 'updated_at', 'random_order', 'collaborative_annotation', 'single_class_classification', - 'tags') - read_only_fields = ('updated_at', 'users', 'current_users_role', 'tags') + fields = ( + 'id', + 'name', + 'description', + 'guideline', + 'users', + 'current_users_role', + 'project_type', + 'updated_at', + 'random_order', + 'collaborative_annotation', + 'single_class_classification', + 'tags' + ) + read_only_fields = ( + 'updated_at', + 'users', + 'current_users_role', + 'tags' + ) class TextClassificationProjectSerializer(ProjectSerializer): - class Meta: + class Meta(ProjectSerializer.Meta): model = TextClassificationProject - fields = ProjectSerializer.Meta.fields - read_only_fields = ProjectSerializer.Meta.read_only_fields class SequenceLabelingProjectSerializer(ProjectSerializer): - class Meta: + class Meta(ProjectSerializer.Meta): model = SequenceLabelingProject - fields = ProjectSerializer.Meta.fields - read_only_fields = ProjectSerializer.Meta.read_only_fields class Seq2seqProjectSerializer(ProjectSerializer): - class Meta: + class Meta(ProjectSerializer.Meta): model = Seq2seqProject - fields = ProjectSerializer.Meta.fields - read_only_fields = ProjectSerializer.Meta.read_only_fields class Speech2textProjectSerializer(ProjectSerializer): - class Meta: + class Meta(ProjectSerializer.Meta): model = Speech2textProject - fields = ('id', 'name', 'description', 'guideline', 'users', 'current_users_role', 'project_type', - 'updated_at', 'random_order') - read_only_fields = ('updated_at', 'users', 'current_users_role') class ImageClassificationProjectSerializer(ProjectSerializer): - class Meta: + class Meta(ProjectSerializer.Meta): model = ImageClassificationProject - fields = ProjectSerializer.Meta.fields - read_only_fields = ProjectSerializer.Meta.read_only_fields class ProjectPolymorphicSerializer(PolymorphicSerializer): model_serializer_mapping = { Project: ProjectSerializer, - TextClassificationProject: TextClassificationProjectSerializer, - SequenceLabelingProject: SequenceLabelingProjectSerializer, - Seq2seqProject: Seq2seqProjectSerializer, - Speech2textProject: Speech2textProjectSerializer, - ImageClassificationProject: ImageClassificationProjectSerializer + **{ + cls.Meta.model: cls for cls in ProjectSerializer.__subclasses__() + } }