Now Django does not have a built-in Price Field; however, a field which simulates money in Django can be easily created using the DecimalField. Django's DecimalField requires 2 parameters, one is max_digits and the other is decimal_places. max_digits is the total number of digits that is in the number specified.
IntegerField is a integer number represented in Python by a int instance. This field is generally used to store integer numbers in the database. The default form widget for this field is a NumberInput when localize is False or TextInput otherwise.
A decimal field is the right choice for the currency value.
It will look something like:
credit = models.DecimalField(max_digits=6, decimal_places=2)
The other answers are 100% right but aren't very practical as you'll still have to manually manage output, formatting etc.
I would suggest using django-money:
from djmoney.models.fields import MoneyField
from django.db import models
def SomeModel(models.Model):
some_currency = MoneyField(
decimal_places=2,
default=0,
default_currency='USD',
max_digits=11,
)
Works automatically from templates:
{{ somemodel.some_currency }}
Output:
$123.00
It has a powerful backend via python-money and it's essentially a drop-in replacement for standard decimal fields.
field = models.DecimalField(max_digits=8, decimal_places=2)
Note that max_digits should be >= decimal_places. This example setting would allow a value up to: 999,999.99
Docs: https://docs.djangoproject.com/en/1.10/ref/models/fields/#decimalfield
Define a decimal and return a $ sign in front of the value.
price = models.DecimalField(max_digits=8, decimal_places=2)
@property
def price_display(self):
return "$%s" % self.price
field = models.DecimalField(max_digits=8, decimal_places=2)
Should create a field for PostgreSQL like:
"field" numeric(8, 2) NOT NULL
Which is the best way for PostGreSQL stored US dollar amount.
If you need a PostgreSQL field type "double precision", then you need do in django model:
field = models.FloatField()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With