BooleanField to checkboxinput in django won't work as it should

484 views Asked by At

I have the following model that has a Boolean field among others

class Prb(models.Model):
    #other fields
    disabled = models.BooleanField(default=False)

and a modelform for the model

class PrbModelForm(ModelForm):

    class Meta:
        model=Prb

In my view I render prbmodelform with a prb as instance

prb = get_object_or_404(Prb, pk=4)
prb_model_form = PrbModelForm(request.POST or None, instance=prb)

prb.disabled field for this model is 0 (using SQLite database) but when I render the form although disabled field is 0 checkbox is checked. How can I solve this problem? I want to add here that disabled is an SQLite Boolean field, and that the data schema was transformed in to models using inspectdb. Why doesn't it render the checkbox status correctly?

EDIT: Queries work as they should, that is when I do this

Prb.objects.filter(disabled=True)

it return the correct prb's. The all have disabled equal to 1.

EDIT#2: value of prb.disabled inside view is 0

form renders as follows(providing only the checkbox field)

<input checked="checked" id="id_disabled" name="disabled" type="checkbox" value="0">
1

There are 1 answers

0
Apostolos On

Just for others to know...The problem was with SQLite. Turns out that when user has not set the field to be not null Django must use a NullBooleanField instead of a plain BooleanField. Migrated from BooleanField to NullBooleanField (to agree with database) and then migrated back to BooleanField to change db's schema. I want everything to be done from the orm and django that's why I went the long way(that is using migrations rather than just altering the field using sql from sqlite command line)

Thank you all...