django - DecimalField max_digits, decimal_places explained

Viewed 4630

So I'm just starting out with Django and using it to store forex prices which are represented as 1.21242, 1.20641, etc...

model.py

from django.db import models

# Create your models here.
class ForexPrice(models.Model):
    openPrice = models.DecimalField(max_digits=6, decimal_places=6)
    highPrice = models.DecimalField(max_digits=6, decimal_places=6)
    lowPrice = models.DecimalField(max_digits=6, decimal_places=6)
    closePrice = models.DecimalField(max_digits=6, decimal_places=6)

My Question Is:
How do the max_decimal and decimal_places attributes work? I'm using 6 for both fields assuming max_digits=6 will allow values to be stored up to 6 digits ie: 123456.000000 while decimal_palces=6 will support values up to 000000.123456.

Is this assumption correct or am I missing something?

Getting the following error when I save the record to the DB:

A field with precision 6, scale 6 must round to an absolute value less than 1.

1 Answers

max_digits must be equal, or higher than decimal_places.

If you were to have 123456.654321 you'd have to define max_digits=12, decimal_places=6.

max_digits is INCLUDING decimal_places.

Related