Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

I tried just adding the max_length, widget and validators to the existing fields, like this:

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.

I tried to look in the django source, but I'm quite new so it's too much to take in right now.

Is there some way I can achieve this?

like image 212
rzetterberg Avatar asked May 06 '13 18:05

rzetterberg


People also ask

What is blank in Django models?

Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the DB. blank determines whether the field will be required in forms. This includes the admin and your custom forms. If blank=True then the field will not be required, whereas if it's False the field cannot be blank.

What does CharField mean in Django?

CharField is a string field, for small- to large-sized strings. It is like a string field in C/C+++. CharField is generally used for storing small strings like first name, last name, etc. To store larger text TextField is used. The default form widget for this field is TextInput.

What is blank true in Django?

If True, Django will store empty values as NULL in the database. Default is False. Blank. If True, the field is allowed to be blank. Default is False.

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.


1 Answers

When creating your form, add the following parameter to CharField apart from the max_length, widget and validators:

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

In Django, blank=True in models correlates to required=False in forms.

like image 169
Alagappan Ramu Avatar answered Sep 21 '22 08:09

Alagappan Ramu