Daniel
Daniel

Reputation: 5389

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

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

Upvotes: 1

Views: 3682

Answers (3)

Pushkar
Pushkar

Reputation: 140

Clearly the default value should be NULL but it will happen only when you activate it i.e. by setting null = True as one of the attributes to the model field. I haven't checked it recently though. However, I recommend setting default value by yourself. e.g. default = 0.000.

Alternatively, you need to pass another attribute similar to blank = True or required = False if you want the model field to be an optional field. Otherwise, it will always prompt you to fill some decimal value in it.

Upvotes: 0

kamyarmg
kamyarmg

Reputation: 904

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)

Upvotes: 3

Lewis
Lewis

Reputation: 2798

Based upon your definition. The default value would be 0.000

If you had keyword argument null=True then you may expect NULL where a value has not been entered in.

Upvotes: 1

Related Questions