How to format money fields in Django?

Asked

Viewed 34 times

0

I have a field in my admin Django that receives a real value, but I would like that value to be formatted and not receive more than two decimal places.

I tried using a javascript to modify the Django admin page, but this generates many side effects:

let valor = document.querySelector('#id_valor');
        let oldVal = '';
        valor.addEventListener('keyup', function(){
            let parts = this.value.split('.');
            console.log(parts)
            if (parts && parts[1] && parts[1].length > 3)
                this.value = oldVal;
                oldVal = this.value;
        });

Is there any better way to do this on Jango?

1 answer

0

Leonardo, in the model itself you could do it

dinheiro = models.DecimalField(max_digits=10, decimal_places=2)

if the user places 3 houses or more, an error message appears indicating the maximum number of decimal places that can be used.

  • I already have this decimal_places active, I wish the user could not type more decimal places, so he does not even see this error.

  • Try using this https://igorescobar.github.io/jQuery-Mask-Pluginplugin

Browser other questions tagged

You are not signed in. Login or sign up in order to post.