Reputation: 43
I am working on a classroom project and I am completely new to Django. How to modify my models (course,user) so that I can add students to the course in two ways 1)By entering the class code by students. 2)By sending join invitations to students from a list of all students by selecting some by the teacher and if the student confirms/accepts he will be added.
i am attaching the models below
user model
class CustomUser(AbstractUser):
email = models.EmailField()
is_teacher = models.BooleanField(default=False)
is_student = models.BooleanField(default=False)
Course Model
class Course(models.Model):
course_name = models.CharField(max_length=200)
course_id = models.CharField(max_length=10)
course_sec = models.IntegerField(validators=[MinValueValidator(1),MaxValueValidator(25)])
classroom_id = models.CharField(max_length=50,unique=True)
created_by = models.ForeignKey(User,on_delete=models.CASCADE)
slug=models.SlugField(null=True, blank=True)
def __str__(self):
return self.course_name
def save(self, *args, **kwargs):
self.slug = slugify(self.classroom_id)
super().save(*args, **kwargs)
def is_valid(self):
pass
Teacher Views
@login_required
def teacher_view(request, *args, **kwargs):
form = add_course(request.POST or None)
context = {}
if form.is_valid():
course = form.save(commit=False)
course.created_by = request.user
course.save()
messages.success(request, "Class Created Sucessfully")
return redirect('/tdashboard')
context['add_courses'] = form
return render(request, 'teacherview.html', context)
@login_required
def view_courses(request, *args, **kwargs):
courses = Course.objects.filter(created_by=request.user)
dict = {'course': courses}
return render(request, 'teacherhome.html', dict)
Upvotes: 0
Views: 111
Reputation: 821
create a many2many relationship between the courses and students. Then you can do course.add(student) when a student has the right invitation code. Is this what you want?
use the groups in django
from django.contrib.auth.models import Group
my_group = Group.objects.get(name='my_group_name')
my_group.user_set.add(your_user)
so you have two groups students and teachers
Upvotes: 1