4

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'}
SkillSet12345
  • 881
  • 3
  • 14
  • 25

2 Answers2

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.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
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.

Denis Biwott
  • 430
  • 5
  • 4