mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Improved subjects model and serializer
This commit is contained in:
parent
3748b4d2c8
commit
3f136f8cef
4 changed files with 84 additions and 11 deletions
|
@ -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'),
|
||||||
|
),
|
||||||
|
]
|
18
stude/subjects/migrations/0004_rename_code_subject_codes.py
Normal file
18
stude/subjects/migrations/0004_rename_code_subject_codes.py
Normal 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',
|
||||||
|
),
|
||||||
|
]
|
|
@ -13,7 +13,8 @@ from semesters.models import Semester
|
||||||
|
|
||||||
class Subject(models.Model):
|
class Subject(models.Model):
|
||||||
name = models.CharField(max_length=64, unique=True)
|
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 = models.ManyToManyField(
|
||||||
'courses.Course', through='subjects.SubjectCourse', related_name='SubjectCourse_subject')
|
'courses.Course', through='subjects.SubjectCourse', related_name='SubjectCourse_subject')
|
||||||
students = models.ManyToManyField(
|
students = models.ManyToManyField(
|
||||||
|
@ -26,7 +27,15 @@ class Subject(models.Model):
|
||||||
'semesters.Semester', through='subjects.SubjectSemester', related_name='SubjectSemester_subject')
|
'semesters.Semester', through='subjects.SubjectSemester', related_name='SubjectSemester_subject')
|
||||||
|
|
||||||
def __str__(self):
|
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):
|
class SubjectCourse(models.Model):
|
||||||
|
@ -117,14 +126,29 @@ def populate_subjects(sender, **kwargs):
|
||||||
semester = Semester.objects.filter(
|
semester = Semester.objects.filter(
|
||||||
name=subject_semester).first()
|
name=subject_semester).first()
|
||||||
# Create the subject instance or get if it already exists
|
# Create the subject instance or get if it already exists
|
||||||
SUBJECT = Subject.objects.get_or_create(
|
if (Subject.objects.filter(name=subject_name).exists()):
|
||||||
name=subject_name,
|
print('Updating existing subject',
|
||||||
code=subject_code,
|
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
|
# 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)
|
print('Added', subject_count, 'subjects from', filename)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from .models import Subject
|
from .models import Subject, SubjectCode
|
||||||
from courses.models import Course
|
from courses.models import Course
|
||||||
from year_levels.models import Year_Level
|
from year_levels.models import Year_Level
|
||||||
from semesters.models import Semester
|
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)
|
queryset=Semester.objects.all(), many=True, slug_field='name', allow_null=True)
|
||||||
courses = serializers.SlugRelatedField(
|
courses = serializers.SlugRelatedField(
|
||||||
queryset=Course.objects.all(), many=True, slug_field='name', allow_null=True)
|
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:
|
class Meta:
|
||||||
model = Subject
|
model = Subject
|
||||||
|
|
Loading…
Reference in a new issue