Final changes to subject and subject instance serializer for easier queries

This commit is contained in:
Keannu Bernasol 2023-09-04 18:18:08 +08:00
parent 6fe1042826
commit 63d16eae17
3 changed files with 19 additions and 14 deletions

View file

@ -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):

View file

@ -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')

View file

@ -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)