Search code examples
pythondjangodjango-forms

Why doesn't MultipleHiddenInput widget get rendered? (Django)


My MultipleHiddenInput field which I am using to store some choices for another field based on a selector doesn't seem to work:

class LogSearch(forms.Form):
    platform = forms.ChoiceField(
         required=True, 
         choices=platforms, 
         initial='v',
         help_text='The platform to search on')

    log_type = forms.MultipleChoiceField(required=True, choices=())
    keyword = forms.CharField(required=True)
    other_log_types = forms.MultipleChoiceField(
        required=False, 
        widget=forms.MultipleHiddenInput, 
        choices=[(1, '3'), (2, 'b')])

I would expect other_log_types to be rendered as hidden. But it doesn't appear at all in my HTML.

I am rendering it properly on the template as all other components appear properly. I'm not sure if I am not using the widget right or not.


Solution

  • I was able to fix this issue by defining in the constructor the following:

    def __init__(self, *args, **kwargs):
        self.fields['other_log_types'].initial = [(1, "example")]
    

    I have no idea why it uses the initial and doesn't work with using choices, but ya.