From 63d16eae17475e62c341e9b7fbed6ef2b2a3e7ea Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Mon, 4 Sep 2023 18:18:08 +0800 Subject: [PATCH] Final changes to subject and subject instance serializer for easier queries --- stude/accounts/serializers.py | 7 +++++-- stude/subjects/serializers.py | 6 ++++-- stude/subjects/views.py | 20 ++++++++++---------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/stude/accounts/serializers.py b/stude/accounts/serializers.py index 986d5df..40874fa 100644 --- a/stude/accounts/serializers.py +++ b/stude/accounts/serializers.py @@ -22,11 +22,14 @@ from drf_extra_fields.fields import Base64ImageField class SubjectSlugRelatedField(serializers.SlugRelatedField): + def to_representation(self, value): + return value.subject.name + def to_internal_value(self, data): user_course = self.context['request'].user.course try: subject = SubjectInstance.objects.get( - name=data, course=user_course) + subject__name=data, course=user_course) return subject except SubjectInstance.DoesNotExist: self.fail('does_not_exist', slug_name=self.slug_field, @@ -47,7 +50,7 @@ class CustomUserSerializer(BaseUserSerializer): many=False, slug_field='name', queryset=Semester.objects.all(), required=False, allow_null=True) # Use custom slug field for filtering subjects = SubjectSlugRelatedField( - many=True, slug_field='name', queryset=SubjectInstance.objects.all(), required=False) + many=True, slug_field='subject', queryset=SubjectInstance.objects.all(), required=False) avatar = Base64ImageField() class Meta(BaseUserSerializer.Meta): diff --git a/stude/subjects/serializers.py b/stude/subjects/serializers.py index e1c7aae..c3971e5 100644 --- a/stude/subjects/serializers.py +++ b/stude/subjects/serializers.py @@ -5,7 +5,8 @@ from year_levels.models import Year_Level from semesters.models import Semester -class SubjectSerializer(serializers.ModelSerializer): +class SubjectInstanceSerializer(serializers.ModelSerializer): + name = serializers.StringRelatedField(source='subject', read_only=True) year_level = serializers.SlugRelatedField( queryset=Subject.objects.all(), slug_field='name', allow_null=False) year_level = serializers.SlugRelatedField( @@ -17,4 +18,5 @@ class SubjectSerializer(serializers.ModelSerializer): class Meta: model = SubjectInstance - fields = ('id', 'subject', 'code', 'course', 'year_level', 'semester') + fields = ('id', 'name', 'code', + 'course', 'year_level', 'semester') diff --git a/stude/subjects/views.py b/stude/subjects/views.py index f08d7a3..170a490 100644 --- a/stude/subjects/views.py +++ b/stude/subjects/views.py @@ -1,20 +1,20 @@ from rest_framework import generics, viewsets from .models import Subject, SubjectInstance -from .serializers import SubjectSerializer +from .serializers import SubjectInstanceSerializer from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.permissions import IsAuthenticated class SubjectListAllView(generics.ListAPIView): - serializer_class = SubjectSerializer + serializer_class = SubjectInstanceSerializer queryset = SubjectInstance.objects.all() class SubjectListView(generics.ListAPIView): permission_classes = [IsAuthenticated] queryset = SubjectInstance.objects.all() - serializer_class = SubjectSerializer + serializer_class = SubjectInstanceSerializer def get(self, request): user = self.request.user @@ -32,13 +32,13 @@ class SubjectListView(generics.ListAPIView): course__name=user.course, year_level__name=user_yearlevel, semester__name=user_semester) # Serialize the subjects - serializer = SubjectSerializer(subjects, many=True) + serializer = SubjectInstanceSerializer(subjects, many=True) return Response(serializer.data) class SubjectByCourseView(generics.ListAPIView): queryset = SubjectInstance.objects.all() - serializer_class = SubjectSerializer + serializer_class = SubjectInstanceSerializer def get(self, request, course_slug): # Retrieve the subjects based on course slug @@ -46,18 +46,18 @@ class SubjectByCourseView(generics.ListAPIView): course__shortname=course_slug) # Serialize the subjects - serializer = SubjectSerializer(subjects, many=True) + serializer = SubjectInstanceSerializer(subjects, many=True) return Response(serializer.data) class SubjectByCourseYearSemesterView(generics.ListAPIView): - queryset = Subject.objects.all() - serializer_class = SubjectSerializer + queryset = SubjectInstance.objects.all() + serializer_class = SubjectInstanceSerializer def get(self, request, course_slug, year_slug, semester_slug): # Retrieve the subjects based on year level and semester slugs - subjects = Subject.objects.filter( + subjects = SubjectInstance.objects.filter( course__shortname=course_slug, year_level__shortname=year_slug, semester__shortname=semester_slug) # Serialize the subjects - serializer = SubjectSerializer(subjects, many=True) + serializer = SubjectInstanceSerializer(subjects, many=True) return Response(serializer.data)