Choices defined for model fields not being enforce

2019-07-21 03:31发布

So I've defined some fields as choices:

class MyModel(models.Model):
    # Text language.                                                                                 
    ENGLISH = 'eng'                                                                                   
    FRENCH  = 'fr'                                                                                   

    LANGUAGES_CHOICES = [                                                                            
        (ENGLISH, 'English'),                                                                        
        (FRENCH, 'French'),                                                                          
    ]                                                                                                

    language = models.CharField(                                                                     
            max_length=max(len(language) for language in LANGUAGES_CHOICES),                         
            choices=LANGUAGES_CHOICES,                                                               
            blank=False,                                                                             
            null=True)

However, I can do MyModel(language='hurhurhur').save() without any error or complaint. What am I missing?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-07-21 03:58

Django validates a model when you validate a modelform, or if you explicitly call modelinst.full_clean() Calling modelinst.save() doesn't validate.

查看更多
登录 后发表回答