Django forms - prepopulate a Boolean field....

Hi again out there,

I am dealing with “Create/Update” part of CRUD in my Django project at the moment and was encountering several Problems I could solve by using google and literature :wink:

But one thing is left that gets me crazy: BooleanFields!

I have several forms in my project to populate the Database…and I am using some of the forms also for altering and updating data. So therefore I prefill the forms in case of update/altering with the corresponding dataset of my database.

My problem? If you have in a ModelForm corresponding to your EmploymentsModel:

  keep_data = forms.BooleanField(label='Daten löschen?', required=False,
                                 widget=forms.RadioSelect(choices=KEEP_DATA_CHOICES, attrs={
                                     'class': 'border border-black border-1 rounded-2 bg-secondary-subtle m-1',
                                     'required': 'True',

and then I try to call the Form again in your with:

(some request if/then things here)
id_alter = request.POST['alterEmployment']
formdata = Employments.objects.get(id=id_alter)
form = EmploymentsForm(instance=formdata)
(some other stuff there, ending in handing over "form" as context)

and sent this from as context to template, I get properly prefilled all form fields except the BooleanForm of “keep_data”…


Sorry, i forgot to include this:

        ["1", "Yes"],
        ["2", "No"],

I think you should use:

       (True, "Yes"),
       (False, "No")

Well…how can I say… OF COURSE I SHOULD :see_no_evil: :see_no_evil: :see_no_evil:

Thx :+1: