StudE-Backend/stude/subjects/serializers.py

28 lines
1 KiB
Python
Raw Normal View History

2023-06-27 18:15:31 +08:00
from rest_framework import serializers
from .models import Subject
2023-06-27 18:42:34 +08:00
from courses.models import Course
2023-07-17 21:23:27 +08:00
from year_levels.models import Year_Level
from semesters.models import Semester
2023-06-27 18:15:31 +08:00
from year_levels.serializers import YearLevelSerializer
from semesters.serializers import SemesterSerializer
2023-06-27 18:42:34 +08:00
from courses.serializers import CourseSerializer
2023-06-27 18:15:31 +08:00
class SubjectSerializer(serializers.ModelSerializer):
2023-07-17 21:23:27 +08:00
year_levels = serializers.SlugRelatedField(
queryset=Year_Level.objects.all(), many=True, slug_field='name', allow_null=True)
semesters = serializers.SlugRelatedField(
queryset=Semester.objects.all(), many=True, slug_field='name', allow_null=True)
2023-06-27 18:42:34 +08:00
courses = serializers.SlugRelatedField(
queryset=Course.objects.all(), many=True, slug_field='name', allow_null=True)
2023-06-27 18:15:31 +08:00
class Meta:
model = Subject
2023-07-17 21:23:27 +08:00
fields = ('name', 'code', 'courses', 'year_levels', 'semesters')
2023-06-27 18:15:31 +08:00
def get_year_level(self, obj):
return obj.year_level.name
def get_semester(self, obj):
return obj.semester.name