Based on two field input of a form need to generate choices for another field dynamically on Django

on _create_mywork_help.html
     *******************************
     
     function generate_daily_time_list() {
    var start_time = document.getElementById("id_start_time").value;
    var interval = Number(document.getElementById("id_interval").value);
    var  daily = document.getElementById("id_daily");
    //document.getElementById("id_daily").value =  document.getElementById("id_daily").defaultValue;
    document.getElementById("id_daily").innerHTML = "";
    console.log(start_time);
    console.log(interval);
    console.log(daily);

    const stime = String(start_time).split(" ");
    const hm = stime[0].split(":");
    console.log(hm[0]);
    console.log(hm[1]);
    var hr = Number(hm[0]);
    var mins = String(hm[1]);
    var pre = 0;
    while (hr > 0){
        pre = hr;
        hr = hr - interval;
    }
    console.log(mins);
    console.log('Deba');
    var i = pre;
    while (i < 24){
        var option= document.createElement("option");
        dtime = String(i).padStart(2, '0')+':'+mins;
        //dtime_v = 'x'+String(i).padStart(2, '0')+mins;
        console.log(dtime)
        i = i+interval;
        option.value= dtime;
        option.text= dtime;
        daily.add(option);
    }
}

     
     
     class myworkAction(workflows.Action):
         start_time = forms.ChoiceField(label=_("Start Time"), required=False)

         interval = forms.CharField(label=_("Hourly"),
                                 required=False,
                                 initial=24,
                                 validators=[validate_scheduler_interval],
                                 widget=forms.TextInput(
                                     attrs={'placeholder': "Repeat interval must be in numbers only (Ex: 1 or 2)...",
                                            'onkeyup': " $('#id_interval').val(document.getElementById('id_interval').value)",
                                            'onchange': "generate_daily_time_list()"}))
         
          daily = forms.MultipleChoiceField(label=_("Daily"), required=False)
    
          class Meta:
        name = _("mywork")
        help_text_template = ("project/workloads/"
                              "_create_mywork_help.html")

    def __init__(self, request, *args, **kwargs):
        super(myworkAction, self).__init__(request,
                                        *args, **kwargs)
        start_time_list = populate_time_list()
        self.fields['start_time'].choices = start_time_list

I want to take input 12:15 PM start time and interval e.g. 2

It should generate choices for “daily” filed as e.g. like 14:15 , 16:15

and it is generating, but when i select and submit it is giving error :

“Select a valid choice. 14:15 is not one of the available choices”. bcz self.fields[‘daily’].choices is blank list , which is not getting updated on the Python layer, however on UI shows all list.

It should update the choice list, but not update.

The choices attribute of a form field must be populated both on the GET for the form and the POST of the form being submitted. Those choices are used to populate the select list for the browser and to validate the submitted data.

Thanks a lot for help Ken, In my case I am populating choice field at browser select list, that means i will update on POST and GET call. am I right? is there other way solve this problem.

This is something that needs to be done in the form. It’s typically done in the __init__ method of the form.

Hi Ken, but how init method get the value of “start_time” and “interval”, as init method stage, yet to user enters input or select choice?

Probably the most “Djangoish” solution I can think of would be to divide this into two separate forms. (Yes, an HTML form can contain multiple Django forms - see The Forms API | Django documentation | Django)

The first form contains the start time and interval, the second form accepts the selections.