Django: how to load a fixture that contains Decimal -
i want load fixture contains decimal. fixture in json "salarymax": "58,689.54", following documentation of django, added true these value:
use_i18n = true use_l10n = true use_thousand_separator = true here snippet of fixture:
{ "fields": { ...#shortened "pub_date": "2015-12-23", "salarymax": "58,689.54", "salarymin": "50,164.66", "salarytype": "annually" }, "model": "emplois.job", "pk": 1 }, here model (for moment salarymax in decimal)
create models here.
@python_2_unicode_compatible class job(models.model): #...shortened joburl = models.urlfield(max_length=250, blank=true, null=true) expirydate = models.datefield(auto_now=true, blank=true, null=true) salarymax = models.decimalfield(max_digits=8, decimal_places=2, localize=true) salarymin = models.charfield(max_length=40, blank=true, null=true) salarytype = models.charfield(max_length=20, blank=true, null=true) name = models.charfield(max_length=40, blank=true, null=true) ... python manage.py loaddata fixtures/data.json here excerpt of error:
file "/home/guinslyziho/development/public/tutorial/django_website_documentation/ottawacitijobs/new_app/ottawacityjobs/emplois/models.py", line 12, in <module> class job(models.model): file "/home/guinslyziho/development/public/tutorial/django_website_documentation/ottawacitijobs/new_app/ottawacityjobs/emplois/models.py", line 34, in job salarymax = models.decimalfield(max_digits=8, decimal_places=2, localize=true) file "/home/guinslyziho/development/public/tutorial/django_website_documentation/env_python_3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1484, in __init__ super(decimalfield, self).__init__(verbose_name, name, **kwargs) typeerror: __init__() got unexpected keyword argument 'localize'
argument localize used forms, not models.
class job(models.model): #...shortened joburl = models.urlfield(max_length=250, blank=true, null=true) expirydate = models.datefield(auto_now=true, blank=true, null=true) salarymax = models.decimalfield(max_digits=8, decimal_places=2) salarymin = models.charfield(max_length=40, blank=true, null=true) salarytype = models.charfield(max_length=20, blank=true, null=true) name = models.charfield(max_length=40, blank=true, null=true) ... proper format of decimal numbers in fixtures are:
float
{ "fields": { ...#shortened "pub_date": "2015-12-23", "salarymax": 58689.54, "salarymin": "50,164.66", "salarytype": "annually" }, "model": "emplois.job", "pk": 1 }, formatted string
{ "fields": { ...#shortened "pub_date": "2015-12-23", "salarymax": '58689.54', "salarymin": "50,164.66", "salarytype": "annually" }, "model": "emplois.job", "pk": 1 }, in case string cannot localized.
try this:
>>> decimal import decimal >>> decimal(58100.23) decimal('58100.2300000000032014213502407073974609375') >>> decimal('58100.23') decimal('58100.23') >>> decimal('58,100.23') traceback (most recent call last): file "<pyshell#4>", line 1, in <module> decimal('58,100.23') file "/usr/lib/python3.5/_pydecimal.py", line 597, in __new__ "invalid literal decimal: %r" % value) file "/usr/lib/python3.5/_pydecimal.py", line 4032, in _raise_error raise error(explanation) decimal.invalidoperation: invalid literal decimal: '58,100.23'
Comments
Post a Comment