Iterate over choices in CheckboxSelectMultiple

后端 未结 2 1966
温柔的废话
温柔的废话 2020-12-15 23:41

I have a CheckboxSelectMultiple field, why can\'t I iterate over the single choices?

This doesn\'t work:

  {%for choice in form.travels.choices%}
            


        
2条回答
  •  独厮守ぢ
    2020-12-16 00:11

    if you have a Form.ModelForm with a choice field, you can itrate it in the template by a simple template filter.

    forms.py

        STATE_CHOICES = (
        (10, 'NO'),
        (4, 'YES'),
        (18, 'Send to another Chemist for Review'),
        (34, 'Send to another Market Expert for Review'),
        (20, 'HOLD'),
    )
    new_state = forms.ChoiceField(
        choices=STATE_CHOICES,
        required=True,
    )
    

    Template:

    {{ business_manager_form.new_state|filter_project_states:project }}
    

    and here is the filter it self.

    @register.filter()
    def filter_project_states(argv, project):
    if project.department.id != 4:
    argv.field.choices = [choice for choice in argv.field.choices if choice[0] != 34]
    return argv
    

    I hope this helps.

提交回复
热议问题