mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-16 22:09:25 +08:00
Added relationship between user and subjects
This commit is contained in:
parent
dc21bb8c32
commit
e02d81f6fd
6 changed files with 67 additions and 3 deletions
19
stude/accounts/migrations/0003_customuser_subjects.py
Normal file
19
stude/accounts/migrations/0003_customuser_subjects.py
Normal file
|
@ -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'),
|
||||
),
|
||||
]
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -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}'
|
||||
|
|
Loading…
Reference in a new issue