Python Django 2.0 DecimalField "Ensure that there are no more than 3 digits before the decimal point"
Asked Answered
F

2

5

My amount column attributes are set to max_digits = 13, decimal_places = 7 because you could technically have something like 10000.0000001 bitcoin.

When I try to enter and submit just 0.1 Bitcoin on my form I get the error:

Ensure that there are no more than 3 digits before the decimal point.

This isn't working as expected: 0.1 is not more than 3 digits, and even if it was I should still be able to set more than 3 digits.. What is happening here?

models.py

class Transactions(models.Model):   
    user = models.ForeignKey(User, on_delete = models.CASCADE)  
    coin = models.CharField(max_length = 64)  
    buysell = models.CharField(default = 'buy', max_length = 4)
    amount = models.DecimalField(max_digits = 13, decimal_places = 7)  
    trade_price = models.DecimalField(max_digits = 5, decimal_places = 2)
    trade_date = models.DateTimeField(auto_now = True) 

forms.py

class TransactionForm(forms.ModelForm): 

    CHOICES = ((1, 'Buy'), (2, 'Sell'),)

    coin = forms.ModelChoiceField(queryset = Coin.objects.all()) 
    buysell = forms.ChoiceField(choices = CHOICES)

    field_order = ['buysell', 'coin', 'amount', 'trade_price']

    class Meta:
        model = Transactions
        fields = {'buysell', 'coin', 'amount', 'trade_price'}
Frantz answered 9/5, 2018 at 10:43 Comment(2)
Try to pass your number like that: number = round(0.1, 7).Eucharis
Are you sure that the backend is getting 0.1. Can you confirm the by checking the request data. It seems like there is something more being sentInbound
F
10

As you say, 0.1 does not have more than 3 digits before the decimal point, so it should not give that error. Therefore the error is probably coming from a different field.

You haven't said what field is giving the error, or what values you submitted for other fields, but I suspect that the problem is your trade_price field.

trade_price = models.DecimalField(max_digits = 5, decimal_places = 2)

Currently, this supports a max value of 999.99. Therefore if you entered trade_price=10000, you would get the no more than 3 digits error.

Fic answered 9/5, 2018 at 11:34 Comment(0)
C
0

Give the decimal value for trade_price like so:

trade_price = "10000.0000001"

I beleive you have been doing it like this:

trade_price = 10000.0000001

The quotation mark is the difference. PS 10000 is a heck ton of Bitcoin.

Constantia answered 4/3, 2022 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.