0

I'd like to convert dollars to cents on save for the Django model.

class Bank(models.Model):
    backup = False
    ...
    device_type = models.BigIntegerField(verbose_name='device type')
    transaction_sum = models.BigIntegerField(verbose_name='sum in dollars') # this field

How can I do it by using the Django Model environment to save it in cents?

UPD: the question is more about getters and setters. Analogy:

enter image description here

I need the same conversion on the fly here.

Павел Иванов
  • 1,863
  • 5
  • 28
  • 51
  • You're unlikely to get many answers without showing your model code or any effort. – rob Oct 06 '21 at 13:37

1 Answers1

1

Override the save method in your model:

class Money (models.Model):
    def save(self, *args, **kwargs):
        # enter code here...
        super(Model, self).save(*args, **kwargs)
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
deVOID
  • 155
  • 7