from django.db import models
class User(models.Model): # Abstract class
username = models.CharField(max_length=100)
dob = models.DateField()
sex = models.CharField(max_length=10)
# Override toString method : so that when we print object of this class, we can see the information like this
def __str__(self):
return f'Username: {self.username}, DOB: {self.dob}, Sex: {self.sex}'
class Teacher(User): # Teacher is also a user
position = models.CharField(max_length=100)
students = models.ManyToManyField('Student', related_name='teachers')
# Override toString method : so that when we print object of this class, we can see the information like this
def __str__(self):
return f'{super().__str__()}, Position: {self.position}'
class Student(User): # Student is also a user
club = models.CharField(max_length=100)
teachers = models.ManyToManyField('Teacher', related_name='students')
# Override toString method : so that when we print object of this class, we can see the information like this
def __str__(self):
return f'{super().__str__()}, Club: {self.club}'
class Course(models.Model):
name = models.CharField(max_length=100)
students = models.ManyToManyField(Student, through='Enrollment')
# Override toString method : so that when we print object of this class, we can see the information like this
def __str__(self):
return f'Course Name: {self.name}'
class Lesson(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
# Override toString method : so that when we print object of this class, we can see the information like this
def __str__(self):
return f'Lesson Name: {self.name}, Course: {self.course.name}'
class Enrollment(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
student = models.ForeignKey(Student, on_delete=models.CASCADE)
# Ensure that they are composite key
class Meta:
unique_together = ('course', 'student')
# Override toString method : so that when we print object of this class, we can see the information like this
def __str__(self):
return f'Course: {self.course.name}, Student: {self.student.username}'