没有对字段选择的验证 Django Postgres?

No Validation on Field Choices Django Postgres?

我创建了一个带有字段选择的学生模型。但是,当我保存它时,它不会验证该选项是否在我在模型字段中指定的选项中。

为什么它不阻止我保存一个新对象,而我没有在我的模型中指定选择?

这是模型:

class Student(models.Model):
    year_in_school = models.CharField(
        max_length=4,
        choices= [
            ('FRES', 'Freshman'),
            ('SOPH', 'Sophomore'),
        ],
    )

这是我在 shell:

中写的代码
>>> from app.models import Student
>>> new_student = Student.objects.create(year_in_school='HACK')
>>> new_student.year_in_school
'HA'

您可能想阅读有关选择的更多信息 here。相关部分复制如下:

If choices are given, they’re enforced by model validation

不会在数据库级别强制执行选择。您需要执行模型验证(通过调用 full_clean())才能对其进行检查。

full_clean() 不会在您调用模型的 save() 方法时自动调用。您需要手动调用它。