我想加载一个包含小数的夹具。夹具是json "salarymax": "58,689.54",
根据django的文档,我添加了这些值:
USE_I18N = True
USE_L10N = True
USE_THOUSAND_SEPARATOR = True
以下是灯具的片段:
{
"fields": {
...#shortened
"pub_date": "2015-12-23",
"salarymax": "58,689.54",
"salarymin": "50,164.66",
"salarytype": "annually"
},
"model": "emplois.job",
"pk": 1
},
这是我的模型(暂时只有salarymax
位于十进制)
@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
这里是错误的摘录:
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 an unexpected keyword argument 'localize'
答案 0 :(得分:2)
参数localize用于表单,而不是模型。
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)
...
灯具中适当的十进制数格式为:
<强>浮强>
{
"fields": {
...#shortened
"pub_date": "2015-12-23",
"salarymax": 58689.54,
"salarymin": "50,164.66",
"salarytype": "annually"
},
"model": "emplois.job",
"pk": 1
},
格式化字符串
{
"fields": {
...#shortened
"pub_date": "2015-12-23",
"salarymax": '58689.54',
"salarymin": "50,164.66",
"salarytype": "annually"
},
"model": "emplois.job",
"pk": 1
},
在这种情况下,字符串无法本地化。
试试这个:
>>> from 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 for Decimal: %r" % value)
File "/usr/lib/python3.5/_pydecimal.py", line 4032, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: '58,100.23'