StudE-Backend/stude/subjects/serializers.py

26 lines
936 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-07-17 21:23:27 +08:00
from year_levels.models import Year_Level
from semesters.models import Semester
from accounts.models import CustomUser
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-18 00:35:41 +08:00
fields = ('id', '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