How to update via AJAX an attribute of a model from the django administrator?

1

Currently I have a problem with the calculation of a value of a model from a database. The calculation is done every time a record is saved or updated but now I must do that calculation in real time and show it before I save the record from the admin interface of django (use django-suit).

models.py

class Poliza(models.Model):
    POLIZAS = (
        ('1', 'AUTO'),
        ('2', 'HOGAR'),
        ('3', 'VIDA'),
        ('4', 'CUMPLIMIENTO'),
        ('5', 'CARGA'),
        ('6', 'SALUD'),
        ('7', 'ARL'),
    )

    cliente = models.ForeignKey(Cliente, on_delete=models.CASCADE)
    aseguradora = models.ForeignKey(Aseguradora, on_delete=models.CASCADE)
    tipo_poliza = models.ForeignKey(TipoPoliza, on_delete=models.CASCADE)#models.CharField(max_length=100, choices=POLIZAS, default='1')
    numero_poliza = models.CharField(max_length=20, blank=True)
    nombre_poliza = models.CharField(max_length=80, blank=True)
    placa_poliza = models.CharField(max_length=20, blank=True)
    fecha_inicio_vigencia = models.DateField(null=True, blank=True)
    fecha_fin_vigencia = models.DateField(null=True, blank=True)
    valor_prima_neta = models.CharField(max_length=50, blank=True)
    otros_gastos = models.CharField(max_length=50, blank=True)
    iva = models.CharField(max_length=50, blank=True)
    valor_total = models.CharField(max_length=50, blank=True)
    observaciones = models.TextField(max_length=1000, blank=True, null=True)
    activo = models.BooleanField(default=True)

    class Meta:
        verbose_name_plural = "Polizas"

    def __unicode__(self):
        return u"%s" % smart_unicode(str(self.cliente).encode('utf-8'))

    def save(self, *args, **kwargs):
        self.valor_total = self.otros_gastos + self.iva + self.valor_prima_neta
        super(Poliza, self).save(*args, **kwargs)

Administration view

Any ideas? Thanks!.

    
asked by Diego Asencio 13.11.2018 в 20:42
source

0 answers