Polished name field for schedule model

This commit is contained in:
keannu125 2023-04-22 14:14:53 +08:00
parent 00c8e6f4f4
commit 716b7cf348
4 changed files with 29 additions and 7 deletions

View file

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

View file

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

View file

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

View file

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