django forms give: Select a valid choice. That choice is not one of the available choices

后端 未结 2 946
情话喂你
情话喂你 2021-02-02 15:50

I am unable to catch the values from the unit_id after the selection is done by the user and data is posted. Can someone help me to solve this.

The values o

2条回答
  •  后悔当初
    2021-02-02 16:17

    Before you call form.is_valid(), do the following:

    1. unit_id = request.POST.get('unit_id')

    2. form.fields['unit_id'].choices = [(unit_id, unit_id)]

    Now you can call form.is_valid() and your form will validate correctly.

提交回复
热议问题