uche
uche

Reputation: 85

How to Set a boolean value to False in django

i have a checkbox button called 'hof' in a form, if i click the checkbox in the form and sends, it works but if i uncheck it refuses to send, what i get is This field is required.

here is my model

class CreateSingleSigner(models.Model):
    firstname = models.CharField(max_length = 32)
    middlename = models.CharField(max_length = 32)
    lastname = models.CharField(max_length = 32)
    displayname = models.CharField(max_length = 32)
    era = models.CharField(max_length = 11)
    category = models.CharField(max_length = 32)
    hof = models.BooleanField()
    externallink = models.CharField(max_length = 70)
    loaprice = models.FloatField(max_length = 10)
    basiccertprice = models.FloatField(max_length = 10)
    appraisalcost = models.FloatField(max_length = 10)
    notability = models.CharField(max_length = 32)

Here if my VIEWS

def singlesigner(request):
    context = {}
    if request.method == 'POST':
        createsinglesigner_form = CreateSingleSignerForm(data=request.POST)
        if createsinglesigner_form.is_valid():

            createsinglesigner = createsinglesigner_form.save(commit=False)
            createsinglesigner.firstname = request.POST['firstname']
            createsinglesigner.middlename = request.POST['middlename']
            createsinglesigner.lastname = request.POST['lastname']
            createsinglesigner.displayname = request.POST['displayname']
            createsinglesigner.era = request.POST['era']
            createsinglesigner.category = request.POST['category']
            createsinglesigner.hof = request.POST['hof']
            createsinglesigner.externallink = request.POST['externallink']
            createsinglesigner.loaprice = request.POST['loaprice']
            createsinglesigner.basiccertprice = request.POST['basiccertprice']
            createsinglesigner.appraisalcost = request.POST['appraisalcost']
            createsinglesigner.notability = request.POST['notability']

            createsinglesigner_form.save()
        else:
                print createsinglesigner_form.errors
    else:
        # context['createsinglesigner'] = CreateSingleSigner()
        createsinglesigner_form =CreateSingleSignerForm()

    return render(request, "signer/singlesigner.html", {"createsinglesigner_form":createsinglesigner_form})

Here is my Form

class CreateSingleSignerForm(forms.ModelForm):
    firstname = forms.CharField(max_length = 32, required = True, help_text = "firstname")
    middlename = forms.CharField(max_length = 32, required = True, help_text = "middlename")
    lastname = forms.CharField(max_length = 32, required = True, help_text = "lastname")
    displayname = forms.CharField(max_length = 32, required = True, help_text = "displayname")
    era = forms.CharField(max_length = 11, required = True, help_text = "era")
    category = forms.CharField(required = True, help_text = 'category')
    hof = forms.BooleanField(required=False, help_text = 'hall of fame')
    notability = forms.CharField(max_length = 11, required = True, help_text = 'notability')
    externallink = forms.CharField(required = True, help_text = "externallink")
    loaprice = forms.FloatField(required = True, help_text = "loaprice $")
    basiccertprice = forms.FloatField(help_text = "basiccertprice $")
    appraisalcost = forms.FloatField(help_text = "appraisalcost $")

    class Meta:
        model = CreateSingleSigner
        fields = ('firstname',)

Upvotes: 0

Views: 1785

Answers (2)

Daniel van Flymen
Daniel van Flymen

Reputation: 11551

You shouldn't be setting an initial value for the BooleanField on your form.

You are also using a ModelForm, it's not necessary to rewrite the logic of your model in the form.

forms.py:

class CreateSingleSignerForm(forms.ModelForm):

    class Meta:
        model = CreateSingleSigner
        fields = (
            'hof',
            ...
        )

    # Instantiate the object and override the required setting on hof field:
    def __init__(self, *args, **kwargs):
        super(CreateSingleSignerForm, self).__init__(*args, **kwargs)
        self.fields['hof'].required = True

If you would like to stick to your code, then inherit from forms.Form instead of forms.ModelForm and instruct Django to save the unchecked state of the checkbox:

hof = forms.BooleanField(required=True, help_text='Hall of Fame')

It's also good practice to set a default value for hof in your models.py for legibility.

Upvotes: 0

zablotski
zablotski

Reputation: 447

Try to change in form

hof = forms.BooleanField(required=False, help_text = 'hall of fame')

to

hof = forms.BooleanField(initial=False, required=False, help_text = 'hall of fame')

Upvotes: 2

Related Questions