diff --git a/stude/accounts/migrations/0003_customuser_subjects.py b/stude/accounts/migrations/0003_customuser_subjects.py new file mode 100644 index 0000000..37f2fd5 --- /dev/null +++ b/stude/accounts/migrations/0003_customuser_subjects.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.2 on 2023-06-28 03:09 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('subjects', '0002_subjectstudent_subject_students'), + ('accounts', '0002_initial'), + ] + + operations = [ + migrations.AddField( + model_name='customuser', + name='subjects', + field=models.ManyToManyField(related_name='SubjectStudent_user', through='subjects.SubjectStudent', to='subjects.subject'), + ), + ] diff --git a/stude/accounts/models.py b/stude/accounts/models.py index 37c8225..24adb8e 100644 --- a/stude/accounts/models.py +++ b/stude/accounts/models.py @@ -54,6 +54,8 @@ class CustomUser(AbstractUser): on_delete=models.CASCADE, null=True ) + subjects = models.ManyToManyField( + 'subjects.Subject', through='subjects.SubjectStudent', related_name='SubjectStudent_user') @property def full_name(self): diff --git a/stude/accounts/serializers.py b/stude/accounts/serializers.py index ca91ceb..79b3298 100644 --- a/stude/accounts/serializers.py +++ b/stude/accounts/serializers.py @@ -1,5 +1,6 @@ from djoser.serializers import UserCreateSerializer as BaseUserRegistrationSerializer from djoser.serializers import UserSerializer as BaseUserSerializer +from rest_framework import serializers from accounts.models import CustomUser from student_status.serializers import StudentStatusSerializer from student_status.models import StudentStatus @@ -12,13 +13,13 @@ class CustomUserSerializer(BaseUserSerializer): class Meta(BaseUserSerializer.Meta): model = CustomUser fields = ('username', 'email', 'password', - 'student_id_number', 'year_level', 'semester', 'course', 'avatar', 'first_name', 'last_name', 'is_banned', 'user_status') + 'student_id_number', 'year_level', 'semester', 'course', 'subjects', 'avatar', 'first_name', 'last_name', 'is_banned', 'user_status') class UserRegistrationSerializer(BaseUserRegistrationSerializer): class Meta(BaseUserRegistrationSerializer.Meta): fields = ('username', 'email', 'password', - 'student_id_number', 'year_level', 'semester', 'course', 'avatar', 'first_name', 'last_name') + 'student_id_number', 'year_level', 'semester', 'course', 'subjects', 'avatar', 'first_name', 'last_name') def create(self, validated_data): # Get the user's year_level and semester from the user model instance diff --git a/stude/subjects/admin.py b/stude/subjects/admin.py index a99cdc9..c57f7c0 100644 --- a/stude/subjects/admin.py +++ b/stude/subjects/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin from courses.models import Course -from .models import Subject +from .models import Subject, SubjectStudent class SubjectAdmin(admin.ModelAdmin): @@ -8,3 +8,4 @@ class SubjectAdmin(admin.ModelAdmin): admin.site.register(Subject, SubjectAdmin) +admin.site.register(SubjectStudent) diff --git a/stude/subjects/migrations/0002_subjectstudent_subject_students.py b/stude/subjects/migrations/0002_subjectstudent_subject_students.py new file mode 100644 index 0000000..e83702f --- /dev/null +++ b/stude/subjects/migrations/0002_subjectstudent_subject_students.py @@ -0,0 +1,29 @@ +# Generated by Django 4.2.2 on 2023-06-28 03:09 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('subjects', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='SubjectStudent', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('subject', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='subjects.subject')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.AddField( + model_name='subject', + name='students', + field=models.ManyToManyField(related_name='SubjectStudent_subject', through='subjects.SubjectStudent', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/stude/subjects/models.py b/stude/subjects/models.py index ece9113..5743157 100644 --- a/stude/subjects/models.py +++ b/stude/subjects/models.py @@ -9,6 +9,8 @@ class Subject(models.Model): code = models.CharField(max_length=16) courses = models.ManyToManyField( 'courses.Course', through='courses.SubjectCourse', related_name='SubjectCourse_subject') + students = models.ManyToManyField( + 'accounts.CustomUser', through='subjects.SubjectStudent', related_name='SubjectStudent_subject') year_level = models.ForeignKey( Year_Level, @@ -24,3 +26,13 @@ class Subject(models.Model): def __str__(self): return self.name + + +class SubjectStudent(models.Model): + user = models.ForeignKey( + 'accounts.CustomUser', on_delete=models.CASCADE) + subject = models.ForeignKey( + 'subjects.Subject', on_delete=models.CASCADE) + + def __str__(self): + return f'User: User={self.user_id}, Subject={self.subject_name}'