Improved subjects model and serializer

This commit is contained in:
Keannu Bernasol 2023-07-19 15:24:59 +08:00
parent 3748b4d2c8
commit 3f136f8cef
4 changed files with 84 additions and 11 deletions

View file

@ -0,0 +1,29 @@
# Generated by Django 4.2.3 on 2023-07-19 07:04
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('subjects', '0002_alter_subject_code_alter_subject_name_and_more'),
]
operations = [
migrations.CreateModel(
name='SubjectCode',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('code', models.CharField(max_length=16, unique=True)),
],
),
migrations.RemoveField(
model_name='subject',
name='code',
),
migrations.AddField(
model_name='subject',
name='code',
field=models.ManyToManyField(to='subjects.subjectcode'),
),
]

View file

@ -0,0 +1,18 @@
# Generated by Django 4.2.3 on 2023-07-19 07:09
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('subjects', '0003_subjectcode_remove_subject_code_subject_code'),
]
operations = [
migrations.RenameField(
model_name='subject',
old_name='code',
new_name='codes',
),
]

View file

@ -13,7 +13,8 @@ from semesters.models import Semester
class Subject(models.Model):
name = models.CharField(max_length=64, unique=True)
code = models.CharField(max_length=16, unique=True)
codes = models.ManyToManyField(
'subjects.SubjectCode')
courses = models.ManyToManyField(
'courses.Course', through='subjects.SubjectCourse', related_name='SubjectCourse_subject')
students = models.ManyToManyField(
@ -26,7 +27,15 @@ class Subject(models.Model):
'semesters.Semester', through='subjects.SubjectSemester', related_name='SubjectSemester_subject')
def __str__(self):
return self.name
code_list = ', '.join(self.codes.values_list('code', flat=True))
return f'{self.name} ({code_list})'
class SubjectCode(models.Model):
code = models.CharField(max_length=16, unique=True)
def __str__(self):
return self.code
class SubjectCourse(models.Model):
@ -117,14 +126,29 @@ def populate_subjects(sender, **kwargs):
semester = Semester.objects.filter(
name=subject_semester).first()
# Create the subject instance or get if it already exists
SUBJECT = Subject.objects.get_or_create(
name=subject_name,
code=subject_code,
if (Subject.objects.filter(name=subject_name).exists()):
print('Updating existing subject',
subject_name, subject_code)
SUBJECT = Subject.objects.filter(name=subject_name
).first()
SUBJECT.courses.add(course)
SUBJECT.year_levels.add(year_level)
SUBJECT.semesters.add(semester)
SUBJECT_CODE = SubjectCode.objects.get_or_create(
code=subject_code)
SUBJECT.codes.add(SUBJECT_CODE[0])
else:
SUBJECT = Subject.objects.get_or_create(
name=subject_name,
)
SUBJECT[0].courses.set([course])
SUBJECT[0].year_levels.set([year_level])
SUBJECT[0].semesters.set([semester])
SUBJECT_CODE = SubjectCode.objects.get_or_create(
code=subject_code)
SUBJECT[0].codes.add(SUBJECT_CODE[0])
subject_count += 1
)
# Set the course, year level, and semester of the subject
SUBJECT[0].courses.set([course])
SUBJECT[0].year_levels.set([year_level])
SUBJECT[0].semesters.set([semester])
subject_count += 1
print('Added', subject_count, 'subjects from', filename)

View file

@ -1,5 +1,5 @@
from rest_framework import serializers
from .models import Subject
from .models import Subject, SubjectCode
from courses.models import Course
from year_levels.models import Year_Level
from semesters.models import Semester
@ -13,6 +13,8 @@ class SubjectSerializer(serializers.ModelSerializer):
queryset=Semester.objects.all(), many=True, slug_field='name', allow_null=True)
courses = serializers.SlugRelatedField(
queryset=Course.objects.all(), many=True, slug_field='name', allow_null=True)
codes = serializers.SlugRelatedField(
queryset=SubjectCode.objects.all(), many=True, slug_field='code', allow_null=False)
class Meta:
model = Subject