StudE-Backend/stude/student_status/serializers.py

32 lines
1.1 KiB
Python
Raw Normal View History

2023-06-26 22:06:05 +08:00
from rest_framework import serializers
from .models import StudentStatus
2023-06-26 22:06:05 +08:00
class StudentStatusSerializer(serializers.ModelSerializer):
2023-07-01 16:09:20 +08:00
year_level = serializers.CharField(
source='user.year_level', read_only=True)
2023-06-27 13:40:31 +08:00
course = serializers.CharField(source='user.course', read_only=True)
semester = serializers.CharField(source='user.semester', read_only=True)
2023-07-01 16:09:20 +08:00
user = serializers.CharField(source='user.full_name', read_only=True)
2023-06-27 13:40:31 +08:00
2023-06-26 22:06:05 +08:00
class Meta:
model = StudentStatus
fields = '__all__'
2023-06-27 13:40:31 +08:00
read_only_fields = ['user']
def create(self, validated_data):
user = self.context['request'].user
2023-06-27 13:40:31 +08:00
student_status = StudentStatus.objects.create(
user=user, defaults=validated_data)
return student_status
2023-07-01 16:09:20 +08:00
2023-06-27 13:40:31 +08:00
def update(self, instance, validated_data):
active = validated_data.get('active', None)
if active is not None and active is False:
validated_data['x'] = None
validated_data['y'] = None
validated_data['subject'] = None
2023-07-01 16:09:20 +08:00
return super().update(instance, validated_data)