From 3f136f8cef86b75e207b4cdb9851ce96262fb12b Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Wed, 19 Jul 2023 15:24:59 +0800 Subject: [PATCH] Improved subjects model and serializer --- ...ctcode_remove_subject_code_subject_code.py | 29 ++++++++++++ .../0004_rename_code_subject_codes.py | 18 ++++++++ stude/subjects/models.py | 44 ++++++++++++++----- stude/subjects/serializers.py | 4 +- 4 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 stude/subjects/migrations/0003_subjectcode_remove_subject_code_subject_code.py create mode 100644 stude/subjects/migrations/0004_rename_code_subject_codes.py diff --git a/stude/subjects/migrations/0003_subjectcode_remove_subject_code_subject_code.py b/stude/subjects/migrations/0003_subjectcode_remove_subject_code_subject_code.py new file mode 100644 index 0000000..f63db3e --- /dev/null +++ b/stude/subjects/migrations/0003_subjectcode_remove_subject_code_subject_code.py @@ -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'), + ), + ] diff --git a/stude/subjects/migrations/0004_rename_code_subject_codes.py b/stude/subjects/migrations/0004_rename_code_subject_codes.py new file mode 100644 index 0000000..6aa4990 --- /dev/null +++ b/stude/subjects/migrations/0004_rename_code_subject_codes.py @@ -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', + ), + ] diff --git a/stude/subjects/models.py b/stude/subjects/models.py index d10b11c..f76a467 100644 --- a/stude/subjects/models.py +++ b/stude/subjects/models.py @@ -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) diff --git a/stude/subjects/serializers.py b/stude/subjects/serializers.py index 5980ba5..a07c22e 100644 --- a/stude/subjects/serializers.py +++ b/stude/subjects/serializers.py @@ -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