Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating django form with null and blank field

Tags:

python

django

I'm trying to create a form where both fields is optional however, i keep getting an error when setting null and blank. what am i doing wrong?

Error

super(CharField, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'blank'

forms.py

class EditProfile(forms.Form):
    """
    A form that lets a user change their profile information
    """
        first_name = forms.CharField(
        label=("Fornavn"),
        strip=False,
        blank=True,
        null=True
    )
    last_name = forms.CharField(
        label=("Efternavn"),
        strip=False,
        blank=True,
        null=True,
    )
    def __init__(self, user, *args, **kwargs):
        self.user = user
        super().__init__(*args, **kwargs)

    def save(self, commit=True):
        first_name = self.cleaned_data["first_name"]
        last_name = self.cleaned_data["last_name"]
        self.user.first_name = first_name
        self.user.last_name = last_name
        if commit:
            self.user.save()
        return self.user
like image 841
Peter Pik Avatar asked Nov 21 '17 20:11

Peter Pik


People also ask

How do I create a field null in Django?

null=True will make the field accept NULL values. Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the database.

What is null and blank in Django?

null is purely database-related, whereas blank is validation-related. If a field has blank=True , form validation will allow entry of an empty value. If a field has blank=False , the field will be required.

How do you make a field optional in Django?

In order to make a field optional, we have to say so explicitly. If we want to make the pub_time field optional, we add blank=True to the model, which tells Django's field validation that pub_time can be empty.

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

As @danielcorreia said you don't use blank in a form, use required = False instead.

class EditProfile(forms.Form):
    """
    A form that lets a user change their profile information
    """
        first_name = forms.CharField(
        label=("Fornavn"),
        strip=False,
        required=False
    )
    last_name = forms.CharField(
        label=("Efternavn"),
        strip=False,
        required=False
    )
    def __init__(self, user, *args, **kwargs):
        self.user = user
        super().__init__(*args, **kwargs)

    def save(self, commit=True):
        first_name = self.cleaned_data["first_name"]
        last_name = self.cleaned_data["last_name"]
        self.user.first_name = first_name
        self.user.last_name = last_name
        if commit:
            self.user.save()
        return self.user
like image 123
epinal Avatar answered Sep 29 '22 11:09

epinal