Moved subject course associative entity to subjects app

This commit is contained in:
Keannu Bernasol 2023-07-10 17:35:54 +08:00
parent 8d9364274c
commit ba7e79cdc2
8 changed files with 86 additions and 16 deletions

View file

@ -1,6 +1,5 @@
from django.contrib import admin
from .models import Course, SubjectCourse
from .models import Course
admin.site.register(Course)
admin.site.register(SubjectCourse)

View file

@ -0,0 +1,21 @@
# Generated by Django 4.2.3 on 2023-07-10 09:34
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('courses', '0002_initial'),
]
operations = [
migrations.RemoveField(
model_name='subjectcourse',
name='course',
),
migrations.RemoveField(
model_name='subjectcourse',
name='subject',
),
]

View file

@ -0,0 +1,22 @@
# Generated by Django 4.2.3 on 2023-07-10 09:34
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('subjects', '0002_subjectcourse_alter_subject_courses'),
('courses', '0003_remove_subjectcourse_course_and_more'),
]
operations = [
migrations.AlterField(
model_name='course',
name='subjects',
field=models.ManyToManyField(related_name='SubjectCourse_course', through='subjects.SubjectCourse', to='subjects.subject'),
),
migrations.DeleteModel(
name='SubjectCourse',
),
]

View file

@ -8,21 +8,12 @@ class Course(models.Model):
name = models.CharField(max_length=64)
shortname = models.CharField(max_length=16)
subjects = models.ManyToManyField(
'subjects.Subject', related_name='SubjectCourse_course', through='courses.SubjectCourse')
'subjects.Subject', related_name='SubjectCourse_course', through='subjects.SubjectCourse')
def __str__(self):
return self.name
class SubjectCourse(models.Model):
subject = models.ForeignKey('subjects.Subject', on_delete=models.CASCADE)
course = models.ForeignKey(
'courses.Course', on_delete=models.CASCADE, null=True)
def __str__(self):
return (self.subject + ' under course ' + self.course)
@receiver(post_migrate)
def populate_courses(sender, **kwargs):
if sender.name == 'courses':

View file

@ -4,7 +4,7 @@ from rest_framework.exceptions import PermissionDenied
from rest_framework.permissions import IsAuthenticated
from .serializers import StudyGroupSerializer
from .models import StudyGroup
from courses.models import SubjectCourse
from subjects.models import SubjectCourse
# Create your views here.

View file

@ -1,6 +1,5 @@
from django.contrib import admin
from courses.models import Course
from .models import Subject, SubjectStudent
from .models import Subject, SubjectStudent, SubjectCourse
class SubjectAdmin(admin.ModelAdmin):
@ -9,3 +8,4 @@ class SubjectAdmin(admin.ModelAdmin):
admin.site.register(Subject, SubjectAdmin)
admin.site.register(SubjectStudent)
admin.site.register(SubjectCourse)

View file

@ -0,0 +1,28 @@
# Generated by Django 4.2.3 on 2023-07-10 09:34
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('courses', '0003_remove_subjectcourse_course_and_more'),
('subjects', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='SubjectCourse',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('course', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='courses.course')),
('subject', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='subjects.subject')),
],
),
migrations.AlterField(
model_name='subject',
name='courses',
field=models.ManyToManyField(related_name='SubjectCourse_subject', through='subjects.SubjectCourse', to='courses.course'),
),
]

View file

@ -8,7 +8,7 @@ class Subject(models.Model):
name = models.CharField(max_length=64)
code = models.CharField(max_length=16)
courses = models.ManyToManyField(
'courses.Course', through='courses.SubjectCourse', related_name='SubjectCourse_subject')
'courses.Course', through='subjects.SubjectCourse', related_name='SubjectCourse_subject')
students = models.ManyToManyField(
'accounts.CustomUser', through='subjects.SubjectStudent', related_name='SubjectStudent_subject')
@ -36,3 +36,12 @@ class SubjectStudent(models.Model):
def __str__(self):
return f'User: User={self.user_id}, Subject={self.subject_name}'
class SubjectCourse(models.Model):
subject = models.ForeignKey('subjects.Subject', on_delete=models.CASCADE)
course = models.ForeignKey(
'courses.Course', on_delete=models.CASCADE, null=True)
def __str__(self):
return (self.subject + ' under course ' + self.course)