简体   繁体   English

为什么django.forms.CharField中缺少“空白”,而django.db.models.CharField中却存在?

[英]Why is “blank” is missing in django.forms.CharField, but present in django.db.models.CharField?

Background 背景

I have a model with two fields that are set the blank: 我有一个模型,其中两个字段设置为空白:

class News(models.Model):                                                                                                                                                                                           
    title = models.CharField(max_length = 50, blank = True)                                                                                                                                                         
    info = models.TextField(blank = True)

The thing is that I want to set the max_length dynamically when the form is built, so I have a custom form: 事实是,我想在构建表单时动态设置max_length ,所以我有一个自定义表单:

class NewsForm(forms.ModelForm):                                                                                                                                                                                    
    def __init__(self, *args, **kwargs):                                                                                                                                                                            
        super(NewsForm, self).__init__(*args, **kwargs)                                                                                                                                                             

        title_max_length = 20                                                                                                                                                                                       
        info_max_length = 100                                                                                                                                                                                       

        self.fields["title"] = forms.CharField(max_length = title_max_length)                                                                                                                                       
        self.fields["info"] = forms.CharField(                                                                                                                                                                      
            widget = forms.Textarea,                                                                                                                                                                                
            validators = [                                                                                                                                                                                          
                MaxLengthValidator(info_max_length)                                                                                                                                                                 
            ]                                                                                                                                                                                                       
        )  

Note: Those two length values are actually fetched from the database, but I chose not to include that code to keep the examples shorter. 注意:这两个长度值实际上是从数据库中获取的,但是我选择不包括该代码以使示例简短。

The problem 问题

When I'm using those custom fields the blank option is overwritten/ignored. 当我使用这些自定义字段时, blank选项将被覆盖/忽略。

I tried just adding the max_length, widget and validators to the existing fields, like this: 我尝试将max_length,小部件和验证器添加到现有字段中,如下所示:

class NewsForm(forms.ModelForm):                                                                                                                                                                                    
    def __init__(self, *args, **kwargs):                                                                                                                                                                            
        super(NewsForm, self).__init__(*args, **kwargs)                                                                                                                                                             

        title_max_length = 20                                                                                                                                                                                       
        info_max_length = 100                                                                                                                                                                                       

        self.fields["title"].max_length = title_max_length                                                                                                                                                          
        self.fields["info"].widget = forms.Textarea                                                                                                                                                                 
        self.fields["info"].validators = [MaxLengthValidator(info_max_length)] 

When doing this the blank option works, but the dynamic max_length is not applied to the form. 这样做时,可以使用blank选项,但是动态max_length不会应用于表单。

I tried to look in the django source, but I'm quite new so it's too much to take in right now. 我尝试查看django来源,但是我很新,因此现在无法接受。

Is there some way I can achieve this? 有什么办法可以做到这一点?

When creating your form, add the following parameter to CharField apart from the max_length , widget and validators: 创建表单时,除了max_length ,小部件和验证器外,向CharField添加以下参数:

forms.CharField(...,required = False)

In Django, blank=True in models correlates to required=False in forms. 在Django中,模型中的blank=True与表单中的required=False相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM