Reputation: 1259
I have those models :
Each student have a personal teacher :
class Student(models.Model):
name= models.CharField(max_length=200, default='', blank=True)
group= models.CharField(max_length=200, default='', blank=True)
teacher= models.ForeignKey(Teacher, null=True, blank=True)
session = models.IntegerField(default=99, blank=True)
def __str__(self):
return self.name
A teacher can teach at one, two, three, fours sessions.
class Teacher(Employe):
...
session1 = models.NullBooleanField(default='', blank=True)
session2 = models.NullBooleanField(default='', blank=True)
session3 = models.NullBooleanField(default='', blank=True)
session4 = models.NullBooleanField(default='', blank=True)
def __str__(self):
return self.name
Each classroom only have one teacher for that particular session.
class Classroom(models.Model):
code = models.CharField(max_length=10, default='', blank=True)
teacherSession1 = models.ForeignKey(Teacher, null=True, blank=True, related_name='S1')
teacherSession2 = models.ForeignKey(Teacher, null=True, blank=True, related_name='S2')
teacherSession3 = models.ForeignKey(Teacher, null=True, blank=True, related_name='S3')
teacherSession4 = models.ForeignKey(Teacher, null=True, blank=True, related_name='S4')
def __str__(self):
return self.code
I want to build a query that would output something like that :
Student name, Student group, Classroom (for session 1 to 4)
So far I have came up with this, but I struggle at getting the classroom code.
def list_student_plan(session):
my_list = Student.objects.values('name', 'group', 'teacher__name').filter(Q(session=session))
return my_list
Upvotes: 0
Views: 471
Reputation: 357
This is my recommend models
Groups
class Groups(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=255, blank=True)
def __str__(self):
return self.name
Student Model
class Student(models.Model):
name = models.CharField(max_length=200, default='', blank=True)
group = models.ForeignKey(Groups, null=True, blank=True, unique=False)
def __str__(self):
return self.name
Code Model
class Code(models.Model):
name = models.CharField(max_length=200)
code = models.CharField(max_length=25)
description = models.CharField(max_length=255)
def __str__(self):
return self.name
Teacher Model
class Teacher(models.Model):
name = models.CharField(max_length=200)
code = models.ForeignKey(Code, unique=False)
def __str__(self):
return self.name + " " + self.code.name
Sessions
class Sessions(Employe):
teacher = models.ForeignKey(Teacher, unique=False)
student = models.ForeignKey(Student, unique=False)
session = models.IntegerField(null=True, blank=True)
def __str__(self):
return self.teacher.name + ' ' + self.student.name
Now with "Sessions" will help you better manage, where session would contain a value between 1-4 ( and possible more - imagine if you added an extra period to the day ). With teacher you will get a code, you can have multiple teachers with multiple codes, or the same teacher with multiple codes. I hope this gets you in the right direction and this in my opinion is a much cleaner way to approach this problem, let me know if you have any issues and I will add more details.
Upvotes: 1