我将如何做?我是 Django 的新手。我已经阅读了很多,但对我来说仍然无法理解。我正在创建一个科目注册模型。我希望它以这种方式过滤:
如果 student_id 存在,则查找主题和课程项(如果存在)将显示错误消息“已存在”,否则将保存条目。
这是我的模型.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.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
    status = models.CharField(max_length=2, choices=STATUS, blank=True, null=True)
    schoolterm = models.ForeignKey(SchoolTerm)
这是我的 admin.py
class SubjectsEnrolledAdmin(admin.ModelAdmin):
    list_display = ('student', 'get_student_lastname', 'get_student_firstname', 'get_student_course', 'subject', 'grade', 'status','schoolterm')
    search_fields = ['student__student_id','student__last_name', 'student__course__course', 'schoolterm__schoolterm']
    def get_student_lastname(self, instance):
        return instance.student.last_name
    get_student_lastname.short_description = 'Last Name'
    def get_student_firstname(self, instance):
        return instance.student.first_name
    get_student_firstname.short_description = 'First Name'
    def get_student_course(self, instance):
        return instance.student.course
    get_student_course.short_description = 'Course'
希望对此有答案...谢谢...