Finalized schedule and student model by adding relationship between both

This commit is contained in:
keannu125 2023-04-22 14:38:27 +08:00
parent 716b7cf348
commit 0169d69914
4 changed files with 30 additions and 19 deletions

View file

@ -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'),
),
]

View file

@ -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
'''

View file

@ -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')

View file

@ -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