From 0169d69914186ca6eafffd764bd166319179d08e Mon Sep 17 00:00:00 2001 From: keannu125 Date: Sat, 22 Apr 2023 14:38:27 +0800 Subject: [PATCH] Finalized schedule and student model by adding relationship between both --- .../migrations/0004_schedule_daytimes.py | 20 +++++++++++++++++++ infotech/schedules/models.py | 17 ++-------------- infotech/schedules/serializers.py | 10 +++++++--- infotech/students/serializers.py | 2 +- 4 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 infotech/schedules/migrations/0004_schedule_daytimes.py diff --git a/infotech/schedules/migrations/0004_schedule_daytimes.py b/infotech/schedules/migrations/0004_schedule_daytimes.py new file mode 100644 index 0000000..490cb0f --- /dev/null +++ b/infotech/schedules/migrations/0004_schedule_daytimes.py @@ -0,0 +1,20 @@ +# Generated by Django 4.2 on 2023-04-22 06:16 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('daytimes', '0001_initial'), + ('schedules', '0003_schedule_name'), + ] + + operations = [ + migrations.AddField( + model_name='schedule', + name='daytimes', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='DayTime_full_name', to='daytimes.daytime'), + ), + ] diff --git a/infotech/schedules/models.py b/infotech/schedules/models.py index 4acbc1a..907a2cd 100644 --- a/infotech/schedules/models.py +++ b/infotech/schedules/models.py @@ -11,8 +11,8 @@ class Schedule(models.Model): 'students.Student', related_name='StudentSchedule_student_assigned', through='schedules.StudentSchedule') professor = models.OneToOneField( 'professors.Professor', related_name='Professor_full_name', on_delete=models.CASCADE) - # daytimes = models.ForeignKey( - # 'daytimes.DayTime', related_name='DayTime_full_name', on_delete=models.CASCADE) + daytimes = models.ForeignKey( + 'daytimes.DayTime', related_name='DayTime_full_name', on_delete=models.CASCADE, null=True) date_created = models.DateTimeField(default=now, editable=False) @@ -33,16 +33,3 @@ class StudentSchedule(models.Model): def __str__(self): return self.schedule - - -''' -class DayTimeSchedule(models.Model): - schedule = models.ForeignKey( - 'schedules.Schedule', on_delete=models.CASCADE) - timeschedule = models.ForeignKey( - 'daytimes.DayTime', on_delete=models.CASCADE) - - def __str__(self): - return self.schedule - -''' diff --git a/infotech/schedules/serializers.py b/infotech/schedules/serializers.py index 6d670a5..8a9209e 100644 --- a/infotech/schedules/serializers.py +++ b/infotech/schedules/serializers.py @@ -2,6 +2,7 @@ from rest_framework import serializers from .models import Schedule from professors.models import Professor from subjects.models import Subject +from daytimes.models import DayTime from students.models import Student @@ -9,8 +10,8 @@ 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(), slug_field='student_assigned', allow_null=True) + students_assigned = serializers.SlugRelatedField( + queryset=Student.objects.all(), many=True, slug_field='full_name', allow_null=True) professor = serializers.SlugRelatedField( queryset=Professor.objects.all(), slug_field='full_name', allow_null=True) @@ -18,8 +19,11 @@ class ScheduleSerializer(serializers.HyperlinkedModelSerializer): 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 - fields = ('id', 'name', 'subject', 'students_assigned', + fields = ('id', 'name', 'subject', 'daytimes', 'students_assigned', 'professor', 'date_created') read_only_fields = ('id', 'date_created', 'name') diff --git a/infotech/students/serializers.py b/infotech/students/serializers.py index beaa322..a45f88e 100644 --- a/infotech/students/serializers.py +++ b/infotech/students/serializers.py @@ -8,7 +8,7 @@ from subjects.models import Subject class StudentSerializer(serializers.HyperlinkedModelSerializer): schedules = serializers.SlugRelatedField( - queryset=Subject.objects.all(), slug_field='name', allow_null=True) + queryset=Subject.objects.all(), many=True, slug_field='name', allow_null=True) class Meta: model = Student