InfoTech-Backend/infotech/schedules/serializers.py

39 lines
1.5 KiB
Python
Raw Permalink Normal View History

from rest_framework import serializers
from .models import Schedule
2023-04-22 11:59:16 +08:00
from professors.models import Professor
from subjects.models import Subject
from daytimes.models import DayTime
2023-04-22 11:59:16 +08:00
from students.models import Student
class ScheduleSerializer(serializers.HyperlinkedModelSerializer):
date_created = serializers.DateTimeField(
format="%d-%m-%Y %I:%M%p", read_only=True)
students_assigned = serializers.SlugRelatedField(
queryset=Student.objects.all(), many=True, slug_field='full_name', allow_null=True)
2023-04-22 11:59:16 +08:00
professor = serializers.SlugRelatedField(
queryset=Professor.objects.all(), slug_field='full_name', allow_null=True)
subject = serializers.SlugRelatedField(
queryset=Subject.objects.all(), slug_field='code', allow_null=True)
daytimes = serializers.SlugRelatedField(
queryset=DayTime.objects.all(), slug_field='name', allow_null=True)
class Meta:
model = Schedule
2023-04-22 15:43:09 +08:00
fields = ('id', 'name', 'max_slots', 'subject', 'year_level', 'semester', 'daytimes', 'students_assigned',
'professor', 'date_created')
2023-04-22 15:43:09 +08:00
read_only_fields = ('id', 'date_created', 'name',
'year_level', 'semester')
def validate(self, attrs):
students = attrs.get('students')
max_slots = attrs.get('max_slots')
if students and students.count() > max_slots:
raise serializers.ValidationError(
'Too many students for this subject')
return attrs