0

这是我的模型.py

class SubjectsEnrolled(models.Model):
student =  models.ForeignKey(Student)
STATUS = (
    ('1', 'Passed'),
    ('2', 'Failed'),
    ('3', 'No Grade'),
    ('4', 'Incomplete'),
)
subject = models.ForeignKey(Subject)
grade = models.CharField(max_length=100, blank=True, null=True)
status = models.CharField(max_length=2, choices=STATUS, blank=True, null=True)
schoolterm = models.ForeignKey(SchoolTerm)

class Meta: 
    verbose_name = _('Subjects Enrolled') 
    verbose_name_plural = _('Subjects Enrolled')

这是我的 Admin.py

class SubjectsEnrolled(models.Model):
student =  models.ForeignKey(Student)
STATUS = (
    ('1', 'Passed'),
    ('2', 'Failed'),
    ('3', 'No Grade'),
    ('4', 'Incomplete'),
)
subject = models.ForeignKey(Subject)
grade = models.CharField(max_length=100, blank=True, null=True)
status = models.CharField(max_length=2, choices=STATUS, blank=True, null=True)
schoolterm = models.ForeignKey(SchoolTerm)

class Meta: 
    verbose_name = _('Subjects Enrolled') 
    verbose_name_plural = _('Subjects Enrolled')

学生在一个学期中应该有一个科目,并且该科目在一个学期中不应重复。

4

1 回答 1

0

使用unique_together元选项。

class Meta:
    unique_together = ('subject', 'schoolterm')

之后验证将在任何地方执行,而不仅仅是在管理员中。

于 2013-04-30T05:11:53.747 回答