Home > Mobile >  How to create a custom student group and add students to that group?
How to create a custom student group and add students to that group?

Time:01-22

I am creating a student project management system and each student will have a group with project. Only the admin will be able to add the students to the specific group using a drop down list menu. So far, I have create a student model and a group model such as these.

class Student(models.Model):
    user = models.OneToOneField(User,null=True,on_delete=models.CASCADE)
    id = models.IntegerField(max_length=11,primary_key=True)
    course_taken = models.CharField(max_length=50,null=True)
    specialization = models.CharField(max_length=50,null=True)
    area_of_interest = models.CharField(max_length=50,null=True) 
    group = models.ForeignKey(Group,null=True)
    def __str__(self):
        if self.user.first_name and self.user.last_name:
            full_name = self.user.first_name   " "   self.user.last_name
        return full_name 

class Group(models.Model):
    id = models.AutoField(primary_key=True)
    members = models.OneToManyField(User,through='Student')
    project_id = models.ForeignKey(Project,null=True)

How to continue from this ?

CodePudding user response:

from django.db import models

class Reporter(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField()

    def __str__(self):
        return "%s %s" % (self.first_name, self.last_name)

class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateField()
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

    def __str__(self):
        return self.headline

    class Meta:
        ordering = ['headline']

As seen in this example you can do that via Model-Relationship (one-to-one, one-to-many, many-to-one, many-to-many). I would suggest consulting the documentation: https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_one/

CodePudding user response:

You have to create create three different class to do that task. With the Enrollment class the admin will be able to add the students to the specific group.

class Student(models.Model):
    name = models.CharField(max_length=30)

    def __str__(self):
        return self.name


class Course(models.Model):
    name = models.CharField(max_length=30)
    student = models.ManyToManyField(Student, through='Enrollment')

    def __str__(self):
        return self.name


class Enrollment(models.Model):
    student = models.ForeignKey(Student, on_delete=models.CASCADE)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    date_enrolled = models.DateField()
    final_grade = models.CharField(max_length=1, blank=True, null=True)


    class Meta:
        unique_together = [['student', 'course']]
  •  Tags:  
  • Related