Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What's the default value for a DecimalField in Django?

I’m adding a new DecimalField to my model, what value will it have by default in the database (if I don’t specify the default explicitly on the field e.g. default=1.23)?

amount = models.DecimalField(max_digits=15, decimal_places=3)

I’m expecting it will be either NULL or 0.0 (or None or Decimal(0.000) in python), but which?

Couldn’t find this mentioned in the docs: https://docs.djangoproject.com/en/2.2/ref/models/fields/#decimalfield

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m using Django 2.2, but expect this is consistent across versions.

>Solution :

Django does not set any default values. You must specify the default value yourself.

amount = models.DecimalField(max_digits=15, decimal_places=3, default=0.0)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading