diff --git a/infotech/schedules/migrations/0003_schedule_name.py b/infotech/schedules/migrations/0003_schedule_name.py new file mode 100644 index 0000000..6d56281 --- /dev/null +++ b/infotech/schedules/migrations/0003_schedule_name.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2 on 2023-04-22 06:12 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('schedules', '0002_alter_schedule_professor'), + ] + + operations = [ + migrations.AddField( + model_name='schedule', + name='name', + field=models.TextField(default='PlaceholderName'), + preserve_default=False, + ), + ] diff --git a/infotech/schedules/models.py b/infotech/schedules/models.py index 4dfcaec..4acbc1a 100644 --- a/infotech/schedules/models.py +++ b/infotech/schedules/models.py @@ -5,6 +5,7 @@ from django.utils.timezone import now class Schedule(models.Model): + name = models.TextField() subject = models.ForeignKey('subjects.Subject', on_delete=models.CASCADE) students_assigned = models.ManyToManyField( 'students.Student', related_name='StudentSchedule_student_assigned', through='schedules.StudentSchedule') @@ -15,12 +16,12 @@ class Schedule(models.Model): date_created = models.DateTimeField(default=now, editable=False) - @property - def name(self): - return f"{self.subject}" + def save(self, *args, **kwargs): + self.name = f"{self.subject} : {self.professor}" + super().save(*args, **kwargs) def __str__(self): - return self.subject + return self.name class StudentSchedule(models.Model): diff --git a/infotech/schedules/serializers.py b/infotech/schedules/serializers.py index 3b1319d..6d670a5 100644 --- a/infotech/schedules/serializers.py +++ b/infotech/schedules/serializers.py @@ -20,6 +20,6 @@ class ScheduleSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Schedule - fields = ('id', 'subject', 'students_assigned', + fields = ('id', 'name', 'subject', 'students_assigned', 'professor', 'date_created') - read_only_fields = ('id', 'date_created') + read_only_fields = ('id', 'date_created', 'name') diff --git a/infotech/students/serializers.py b/infotech/students/serializers.py index ade9ce2..beaa322 100644 --- a/infotech/students/serializers.py +++ b/infotech/students/serializers.py @@ -1,12 +1,14 @@ +from django.shortcuts import get_object_or_404 from rest_framework import serializers from django.contrib.auth.models import User from .models import Student from schedules.models import Schedule +from subjects.models import Subject class StudentSerializer(serializers.HyperlinkedModelSerializer): schedules = serializers.SlugRelatedField( - queryset=Schedule.objects.all(), slug_field='schedule_subject', allow_null=True) + queryset=Subject.objects.all(), slug_field='name', allow_null=True) class Meta: model = Student