Advertisement
horozov86

django relations 2

Nov 16th, 2023
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. GRADE_CHOICES = (
  2.     ("A", "A"),
  3.     ("B", "B"),
  4.     ("C", "C"),
  5.     ("D", "D"),
  6.     ("F", "F"),
  7. )
  8.  
  9. class Lecturer(models.Model):
  10.     first_name = models.CharField(max_length=100)
  11.     last_name = models.CharField(max_length=100)
  12.  
  13.     def __str__(self):
  14.         return f"{self.first_name} {self.last_name}"
  15.  
  16.  
  17. class Subject(models.Model):
  18.     name = models.CharField(max_length=100)
  19.     code = models.CharField(max_length=10)
  20.     lecturer = models.ForeignKey("Lecturer", on_delete=models.SET_NULL, null=True)
  21.  
  22.     def __str__(self):
  23.         return f"{self.name}"
  24.  
  25.  
  26. class Student(models.Model):
  27.     student_id = models.CharField(max_length=10, primary_key=True)
  28.     first_name = models.CharField(max_length=100)
  29.     last_name = models.CharField(max_length=100)
  30.     birth_date = models.DateField()
  31.     email = models.EmailField(unique=True)
  32.     subjects = models.ManyToManyField(to='Subject',
  33.                                       through='StudentEnrollment')
  34.  
  35. class StudentEnrollment(models.Model):
  36.     student = models.ForeignKey(to='Student',
  37.                                 on_delete=models.CASCADE)
  38.     subject = models.ForeignKey(to='Subject',
  39.                                 on_delete=models.CASCADE)
  40.     enrollment_date = models.DateField(default=date.today)
  41.     grade = models.CharField(max_length=1, choices=GRADE_CHOICES,
  42.                              blank=True, null=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement