0

I have models

class tipo_pago(models.Model):
    descripcion = models.CharField(max_length=20)
    banco_req   = models.BooleanField(default=False, verbose_name="Requiere banco", help_text="Activa esta casilla si se requiere el banco de origen")
    status      = models.BooleanField(default=True)

and

class cliente_pago(models.Model):
    tEstado = (
        ('pagado','Pagado'),
        ('pendiente','Pendiente'),
        ('cancelado','Cancelado'),
    )

    cliente     = models.ForeignKey('cliente',blank=False,null=True,on_delete=models.SET_NULL)
    fecha       = models.DateTimeField(default=timezone.now)
    fecha_pago  = models.DateField()
    tipo_pago   = models.ForeignKey('tipo_pago',blank=False,null=True,on_delete=models.SET_NULL)
    banco       = models.ForeignKey('banco', blank=True, null=True,on_delete=models.SET_NULL)
    referencia  = models.CharField(max_length=25)
    cuenta      = models.ForeignKey('cuenta',blank=False,null=True,on_delete=models.SET_NULL)
    importe     = models.FloatField(default=0)
    pago        = models.FloatField(default=0)
    saldo       = models.FloatField(default=0)
    observacion = models.TextField(blank=True,null=True)
    estado      = models.CharField(max_length=10,default='pendiente',choices=tEstado)

My form to fill the fileds is the next one:

class cliente_pagoModal(CustomModelForm):

    class Meta:
        model   = cliente_pago
        exclude = ('fecha','pago','saldo','estado',)

To the field tipo_pago in cliente_pago form i want to add a data parameter

Now look like this:

<select name="tipo_pago" class="form-control" required="" id="id_tipo_pago">
  <option value="" selected="">---------</option>
  <option value="3">Efectivo</option>
  <option value="2">Cheque</option>
  <option value="1">Transferencia bancar</option>
</select>

but I want it to look like this

<select name="tipo_pago" class="form-control" required="" id="id_tipo_pago">
  <option value="" selected="">---------</option>
  <option value="3" data-banco_req="0">Efectivo</option>
  <option value="2" data-banco_req="1">Cheque</option>
  <option value="1" data-banco_req="2">Transferencia bancar</option>
</select>

I am using python 3.7 and django 2.0.8

Ledorub
  • 354
  • 3
  • 9

1 Answers1

0

If the value of data-banco_req is a count of the loops, you can build the select element in your template and iterate over the choices:

{% with field=form.tipo_pago %}
<label for="{{ field.auto_id }}">{{ field.label }}</label> 
<select name="{{ field.html_name }}" class="form-control" required="" id="{{ field.id_for_label }}">
  <option value="" selected="">---------</option>
  {% for choice in field %}
  <option value="{{ choice.data.value }}" data-banco_req="{{ forloop.counter0 }}">{{ choice.choice_label }}</option>
  {% endfor %}
</select>
{% endwith %}
MattRowbum
  • 2,162
  • 1
  • 15
  • 20