StudE-Backend/stude/subjects/serializers.py

24 lines
799 B
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-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):
year_level = serializers.SerializerMethodField()
semester = serializers.SerializerMethodField()
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-06-27 18:42:34 +08:00
fields = ('name', 'code', 'courses', 'year_level', 'semester')
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