Initialize forms.Select wizard

I would like to initialize a form field (owner) with a 2-tuple in get_form_initial. For testing purposes, I’ve hardcoded initial to the tuple, but the owner select field is never populated. I eventually will retrieve data from the db for initializing the owner field.

However, I am able to initialize the owner field with a single value when forms.Select is changed to forms.TextInput, but never with a 2-tuple when owner is defined as a forms.Select.

I will really appreciate any help on this.

Thanks

models.py (partial)

image

forms.py (partial)

image

views.py

The initial data for a form represent the existing values of the data, not the options available to be selected.

Where are you setting the choices attribute for the owner field?

Ken, still learning Django, but now I understand.

I initially thought I could just define the model and form field, and then in the wizard step, pre-populate the owner field in the form. Would it be best to perform a queryset in the form, and then assign the choice values to owner?

Thanks for your assistance!

I’m not sure I’m quite following you. You can prepopulate the value of the owner field. However, if the field is a Select field, then the value being set must be one of the choices.

When you use a Select field, it will assign the value selected in the form to the model field. That’s not something you need to do yourself.

Thanks, Ken. I’m going to investigate changing owner to ChoiceField.

Cheers

Again, I’m not sure I’m following you here. First, I mistakenly called Choice field a “Select” field. (It’s a mistake I make often.) The field is a Choice field, the widget is the Select widget.

So there’s nothing to be “changed” here in the form.

Hi Ken, got it working!!! Much appreciated for your insight.

I changed my owner field to:

image

and it now updates dynamically. One issue though. I lost my formatting for my owner widget.

image

Any ideas how this can be implemented?

Thanks again!

Cheers

Include the attrs attribute in your forms.Select( constructor with the choices=... attribute.

Formatting is back - thanks!

I did try this initially, but I inadvertently added the attributes outside of the enclosed “choices”, instead of within the Select params.

Take care, and thanks again.

Cheers,
Dave