Django多个配置文件

时间:2018-04-10 22:40:22

标签: django python-3.x relational-database

学生以什么方式向特定教师提交作业(教授特定课程)......他们有什么关系?

models.py

class Student(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    avatar = models.ImageField(upload_to='media/pictures/', blank=True)
    regex = RegexValidator(r'^\d{2}/\d{4}',
                           message='Required. 7 characters or fewer. This value must contain only numeric and slash.')
    matric_no = models.CharField(validators=[regex], blank=True, null=True, max_length=7, unique=True)
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',
                                 message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
    phone_number = models.CharField(validators=[phone_regex], max_length=17, blank=True)  # validators should be a list
    address = models.CharField(max_length=150, blank=True)
    birth_date = models.DateField(null=True, blank=True)

    GENDER_CHOICES = (
            ('M', 'Male'),
            ('F', 'Female'),
        )


class Teacher(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    avatar = models.ImageField(upload_to='media/pictures/', blank=True)
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',
                                 message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
    phone_number = models.CharField(validators=[phone_regex], max_length=17, blank=True)  # validators should be a list
    address = models.CharField(max_length=150, blank=True)
    birth_date = models.DateField(null=True, blank=True)
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, null=False, default='M',)

    def __str__(self):
        return str(self.user)

可能是用户短信或邮寄(用户对用户)?

1 个答案:

答案 0 :(得分:1)

如果一个Student只能将一个给定的作业只提交给一个Teacher(即不是一组教师),您可以使用类似以下的模型:

class Assignment(models.Model):
    student = models.ForeignKey(Student)
    teacher = models.ForeignKey(Teacher)
    ...other fields...

您还可以在ManyToManyStudent模型之间使用Teacher关系并创建自己的through表,但在语义上,上面的方法在这种情况下更有意义。< / p>