mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2025-06-29 00:35:45 +08:00
Initial fix for duplicate subject queries in student status
This commit is contained in:
parent
451c5ec00b
commit
6fe1042826
20 changed files with 87 additions and 95 deletions
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-08-06 05:55
|
||||
# Generated by Django 4.2.3 on 2023-09-03 09:32
|
||||
|
||||
import accounts.models
|
||||
import django.contrib.auth.models
|
||||
|
@ -13,9 +13,9 @@ class Migration(migrations.Migration):
|
|||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('auth', '0012_alter_user_first_name_max_length'),
|
||||
('semesters', '0001_initial'),
|
||||
('courses', '0001_initial'),
|
||||
('auth', '0012_alter_user_first_name_max_length'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
|
@ -36,7 +36,7 @@ class Migration(migrations.Migration):
|
|||
('is_student', models.BooleanField(default=True)),
|
||||
('is_studying', models.BooleanField(default=False)),
|
||||
('irregular', models.BooleanField(default=False)),
|
||||
('student_id_number', models.IntegerField()),
|
||||
('student_id_number', models.IntegerField(unique=True)),
|
||||
('avatar', models.ImageField(null=True, upload_to=accounts.models.CustomUser._get_upload_to)),
|
||||
('course', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='courses.course')),
|
||||
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-08-06 05:55
|
||||
# Generated by Django 4.2.3 on 2023-09-03 09:32
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
@ -9,17 +9,17 @@ class Migration(migrations.Migration):
|
|||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('auth', '0012_alter_user_first_name_max_length'),
|
||||
('accounts', '0001_initial'),
|
||||
('subjects', '0001_initial'),
|
||||
('year_levels', '0001_initial'),
|
||||
('subjects', '0001_initial'),
|
||||
('auth', '0012_alter_user_first_name_max_length'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='customuser',
|
||||
name='subjects',
|
||||
field=models.ManyToManyField(to='subjects.subject'),
|
||||
field=models.ManyToManyField(to='subjects.subjectinstance'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='customuser',
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
# Generated by Django 4.2.3 on 2023-09-02 05:03
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('accounts', '0002_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='customuser',
|
||||
name='student_id_number',
|
||||
field=models.IntegerField(unique=True),
|
||||
),
|
||||
]
|
|
@ -59,7 +59,7 @@ class CustomUser(AbstractUser):
|
|||
on_delete=models.SET_NULL,
|
||||
null=True
|
||||
)
|
||||
subjects = models.ManyToManyField('subjects.Subject')
|
||||
subjects = models.ManyToManyField('subjects.SubjectInstance')
|
||||
|
||||
@property
|
||||
def full_name(self):
|
||||
|
|
|
@ -11,7 +11,7 @@ from django.contrib.auth.password_validation import validate_password
|
|||
from courses.models import Course
|
||||
from year_levels.models import Year_Level
|
||||
from semesters.models import Semester
|
||||
from subjects.models import Subject
|
||||
from subjects.models import Subject, SubjectInstance
|
||||
from django.contrib.gis.geos import Point
|
||||
from django.utils.encoding import smart_str
|
||||
from drf_spectacular.utils import extend_schema_field
|
||||
|
@ -25,9 +25,10 @@ class SubjectSlugRelatedField(serializers.SlugRelatedField):
|
|||
def to_internal_value(self, data):
|
||||
user_course = self.context['request'].user.course
|
||||
try:
|
||||
subject = Subject.objects.get(name=data, course=user_course)
|
||||
subject = SubjectInstance.objects.get(
|
||||
name=data, course=user_course)
|
||||
return subject
|
||||
except Subject.DoesNotExist:
|
||||
except SubjectInstance.DoesNotExist:
|
||||
self.fail('does_not_exist', slug_name=self.slug_field,
|
||||
value=smart_str(data))
|
||||
except (TypeError, ValueError):
|
||||
|
@ -46,7 +47,7 @@ class CustomUserSerializer(BaseUserSerializer):
|
|||
many=False, slug_field='name', queryset=Semester.objects.all(), required=False, allow_null=True)
|
||||
# Use custom slug field for filtering
|
||||
subjects = SubjectSlugRelatedField(
|
||||
many=True, slug_field='name', queryset=Subject.objects.all(), required=False)
|
||||
many=True, slug_field='name', queryset=SubjectInstance.objects.all(), required=False)
|
||||
avatar = Base64ImageField()
|
||||
|
||||
class Meta(BaseUserSerializer.Meta):
|
||||
|
@ -112,7 +113,7 @@ class CustomUserSerializer(BaseUserSerializer):
|
|||
|
||||
def add_subjects(self, instance):
|
||||
# Get the matching subjects based on the user's course, year level, and semester
|
||||
matching_subjects = Subject.objects.filter(
|
||||
matching_subjects = SubjectInstance.objects.filter(
|
||||
course=instance.course, year_level=instance.year_level, semester=instance.semester)
|
||||
# Add the matching subjects to the user's subjects list
|
||||
print(matching_subjects)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue