Skip to content
Advertisement

How to store bitcoin and USD on the same django models field?

To store USD I am using DecimalField:

    amount = models.DecimalField(
        max_digits=12,
        decimal_places=2,
    )

But what if I want to store bitcoins? Bitcoin’s minimal thing is satoshi. One bitcoin = million satoshi. I have 2 options: using DecimalField with decimal_places=5 or using an IntegerField. But I do not want to have a sepperate field for storing this. Is there any way to organize storing bitcoin and USD in the same field?

Advertisement

Answer

If you store two different currencies in the field, you will have a second field that stores the currency, otherwise it is already wrong to begin with.

amount = models.DecimalField(max_digits=12, decimal_places=2)
currency = models.CharField(max_length=3)

So simply use your currency field to differenciate between BTC and SAT(oshi).

Alternatively, you could never store a value in BTC and always store Satoshi. But then you would still need to be able to differenciate between USD and SAT.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement