From 288a1edd5e50add424c272277ef894c3c7d0946e Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Thu, 6 Jul 2023 21:47:28 +0800 Subject: [PATCH] Improved serializers --- stude/accounts/serializers.py | 12 +++++++++--- stude/study_groups/serializers.py | 6 ++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/stude/accounts/serializers.py b/stude/accounts/serializers.py index 94518b1..90f3b54 100644 --- a/stude/accounts/serializers.py +++ b/stude/accounts/serializers.py @@ -8,14 +8,20 @@ from student_status.serializers import StudentStatusSerializer from student_status.models import StudentStatus from rest_framework.settings import api_settings from django.contrib.auth.password_validation import validate_password +from courses.models import Course +from year_levels.models import Year_Level +from semesters.models import Semester class CustomUserSerializer(BaseUserSerializer): user_status = StudentStatusSerializer( source='studentstatus', read_only=True) - course = serializers.StringRelatedField() - year_level = serializers.StringRelatedField() - semester = serializers.StringRelatedField() + course = serializers.SlugRelatedField( + many=False, slug_field='name', queryset=Course.objects.all(), required=False, allow_null=True) + year_level = serializers.SlugRelatedField( + many=False, slug_field='name', queryset=Year_Level.objects.all(), required=False, allow_null=True) + semester = serializers.SlugRelatedField( + many=False, slug_field='name', queryset=Semester.objects.all(), required=False, allow_null=True) class Meta(BaseUserSerializer.Meta): model = CustomUser diff --git a/stude/study_groups/serializers.py b/stude/study_groups/serializers.py index bb3b0fe..5c1ec6e 100644 --- a/stude/study_groups/serializers.py +++ b/stude/study_groups/serializers.py @@ -1,12 +1,14 @@ from rest_framework import serializers from .models import StudyGroup, StudyGroupMembership from accounts.models import CustomUser +from subjects.models import Subject class StudyGroupSerializer(serializers.ModelSerializer): users = serializers.SlugRelatedField( - queryset=CustomUser.objects.all(), many=True, slug_field='name', allow_null=True) - subject = serializers.CharField(source='subject.Subject', read_only=True) + queryset=CustomUser.objects.all(), many=True, slug_field='name', required=False, allow_null=True) + subject = serializers.SlugRelatedField( + many=False, slug_field='name', queryset=Subject.objects.all(), required=True, allow_null=False) class Meta: model = StudyGroup