Home > Software design >  Django Model Choice Field select not working
Django Model Choice Field select not working

Time:01-20

I have the following form:

forms.py

class TipoDePagoForm(forms.Form):
    tipo_de_pago = forms.ChoiceField(widget=forms.RadioSelect, choices=FORMAS_PAGO, required=True)
    folio = forms.CharField(widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': 'Folio',
        'aria-describedby': 'basic-addon2'
    }), required= False)
    medio_venta = forms.ModelChoiceField(queryset= MediosVenta.objects.all())

the ModelChoiceField in HTML is represented like this:

 <form method="POST" >
              {% csrf_token %}
                <h3>Opciones de Pago</h3>
                  <div >
                    {% for value, name in tipodepagoform.fields.tipo_de_pago.choices %}
                    <div >
                      <input id="{{ name }}" name="tipo_de_pago" value="{{ value }}" type="radio"  required>
                      <label  for="{{ name }}">{{ name }}</label>
                    </div>
                    {% endfor %}
                  </div>
                  <div >
                    <label for="folio">{{ tipodepagoform.folio }}</label>
                  </div>
                  <div >
                    <select name="{{ tipodepagoform.medio_venta.nombre }}" id="{{ tipodepagoform.medio_venta.id_for_label }}">
                      {% for value, name in tipodepagoform.fields.medio_venta.choices %}
                        <option value="{{ value }}" {% if tipodepagoform.medio_venta.value == value  %} selected {% endif %}>{{ name }}</option>
                      {% endfor %}
                    </select>
                  </div>

                  <hr >
                  <button  type="submit" >Pagar</button>
            </form>

when doing the POST in the view (part of the view):

def post(self, *args, **kwargs):
        today = date.today()
        tipodepagoform = TipoDePagoForm(self.request.POST or None)
        
        

        if tipodepagoform.is_valid():
            tipo_de_pago = tipodepagoform.cleaned_data.get('tipo_de_pago')
            folio = tipodepagoform.cleaned_data.get('folio')
            medio_venta = tipodepagoform.cleaned_data.get('medio_venta')
            print(medio_venta)

the form_is is not valid and is giving me the following error:

<ul ><li>medio_venta<ul ><li>This field is required.</li></ul></li></ul>

printing the form I see that is selecting the None value:

<tr><th><label for="id_medio_venta">Medio venta:</label></th><td><ul ><li>This field is required.</li></ul><select name="medio_venta" required id="id_medio_venta">
  <option value="" selected>---------</option>

  <option value="1">Gimnasio</option>

  <option value="2">Marketplace</option>

  <option value="3">Membresías</option>

</select></td></tr>

I don´t know why even if I select any option with a value it does not actually select.

Post print:

<QueryDict: {'XXXXXXXXXX], 'tipo_de_pago': ['Efectivo'], 'folio': ['']}>

the post is not getting the model choice field

CodePudding user response:

i think you have a error here:

<select name="{{ tipodepagoform.medio_venta.nombre }}"

Try to change it on the:

<select name="{{ tipodepagoform.medio_venta.name }}"
  • Related