本文介绍了Model.objects.create执行一个full_clean的等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有以下代码:

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)

当我做 MyModel(language ='invalid')。save() MyModel.objects.create(language ='invalid')没有投诉有没有任何方便的方法相当于 Model.objects.create 在保存之前执行 full_clean p>

When I do MyModel(language='invalid').save() or MyModel.objects.create(language='invalid'), the model is saved without complaint. Is there any convenience method that is equivalent to Model.objects.create that performs a full_clean before it saves?

推荐答案

如果您使用模型表单, full_clean 将被称为部分的表单验证步骤。

If you are using model forms, the full_clean will be called as part of the form validation step.

一般来说,没有一个方便的方法调用 full_clean ,但你可以自己写。

In general, there isn't a convenience method that calls full_clean, but you could write your own.

这篇关于Model.objects.create执行一个full_clean的等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 13:14