pythondjangodjango-modelsdjango-2.0

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


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'}

Solution

  • 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.