Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“Django”;十进制字段必须定义一个';小数位数';属性;_Django - Fatal编程技术网

“Django”;十进制字段必须定义一个';小数位数';属性;

“Django”;十进制字段必须定义一个';小数位数';属性;,django,Django,我的模型字段之一如下所示: aaf_1kg_all = models.DecimalField(blank=True, null=True) 当我正常使用我的模型时,一切都很好。但是,当我在中使用它时,会出现以下错误: SystemCheckError: System check identified some issues: ERRORS: myapp.Model.aaf_1kg_all: (fields.E130) DecimalFields must define a 'decimal

我的模型字段之一如下所示:

aaf_1kg_all = models.DecimalField(blank=True, null=True)
当我正常使用我的模型时,一切都很好。但是,当我在中使用它时,会出现以下错误:

SystemCheckError: System check identified some issues:

ERRORS:
myapp.Model.aaf_1kg_all: (fields.E130) DecimalFields must define a 'decimal_places' attribute.
myapp.Model.aaf_1kg_all: (fields.E132) DecimalFields must define a 'max_digits' attribute.
Django文档说这两个属性是可选的。我看到了,但是在数据库中,小数位和最大位数都被定义了

如果我决定添加这些属性,即

aaf_1kg_all = models.DecimalField(blank=True, null=True, max_digits=10, decimal_places=10)
应用程序正在运行,但在某个时候,我却出现了以下错误:

Traceback (most recent call last):
[...]
  variants.extend(list(sub_qs))   # sub_qs is a QuerySet
File ".../django/db/models/query.py", line 258, in __iter__
  self._fetch_all()
File ".../django/db/models/query.py", line 1074, in _fetch_all
  self._result_cache = list(self.iterator())
File ".../django/db/models/query.py", line 68, in __iter__
  for row in compiler.results_iter(results):
File ".../django/db/models/sql/compiler.py", line 808, in results_iter
  row = self.apply_converters(row, converters)
File ".../django/db/models/sql/compiler.py", line 792, in apply_converters
  value = converter(value, expression, self.connection, self.query.context)
File ".../django/db/backends/sqlite3/operations.py", line 233, in convert_decimalfield_value
  value = expression.output_field.format_number(value)
File ".../django/db/models/fields/__init__.py", line 1608, in format_number
  return utils.format_number(value, self.max_digits, self.decimal_places)
File ".../django/db/backends/utils.py", line 200, in format_number
  value = value.quantize(decimal.Decimal(".1") ** decimal_places, context=context)
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
回溯(最近一次呼叫最后一次):
[...]
extend(list(sub#qs))sub#qs是一个查询集
文件“../django/db/models/query.py”,第258行,在iter中__
self._fetch_all()
文件“../django/db/models/query.py”,第1074行,在_fetch_all中
self.\u result\u cache=list(self.iterator())
文件“../django/db/models/query.py”,第68行,在iter中__
对于编译器中的行。结果\u iter(结果):
文件“../django/db/models/sql/compiler.py”,第808行,在results\u iter中
行=自身。应用转换器(行,转换器)
文件“../django/db/models/sql/compiler.py”,第792行,在apply\u转换器中
value=converter(值、表达式、self.connection、self.query.context)
文件“../django/db/backends/sqlite3/operations.py”,第233行,在convert\u decimalfield\u值中
值=表达式。输出\字段。格式\编号(值)
文件“../django/db/models/fields/_init__.py”,第1608行,格式为\u编号
返回utils.format\u number(值、self.max\u位、self.decimal\u位)
文件“../django/db/backends/utils.py”,第200行,格式为\u编号
value=value.quantize(十进制.decimal(“.1”)**小数位,context=context)
decimal.invalidooperation:[]

我忘了什么吗?

您的
最大位数应该大于
小数位数

请注意,
0.100000000
使用10位数字,但
1.0000000000
使用11位数字。因此,如果您有
max_digits=10
小数位=10
,则任何大于或等于
1.0
的数字都将给出错误

如果你不需要小数点,也许你需要像
max_digits=10
decimal_digits=3
。或者,如果您确实需要
小数位数=10
,那么您可能需要类似于
最大位数=12
,这将支持小于1000的值


Django 1.9添加了一个验证器,试图防止类似这样的错误。有关更多详细信息,请参阅

参数
最大位数
必须大于
小数位

e、 g

十进制列不允许值大于列定义所暗示的范围。例如,十进制(3,0)列支持-999到999的范围。十进制(M,D)列允许小数点左边最多有M-D位

如果您想接受数字和小数位置的10位数字,您需要像这样声明“aaf_1kg_all” 小数点(10位)


max\u digits
decimal\u places
参数是可选的。他们是必需的。这个链接将帮助你解决你的错误。我不理解反对票,但得到答案才是最重要的。谢谢大家。如果
max\u digits
确实必须大于
decimal\u places
,那么这就是文档错误。根据,
请注意,此数字必须大于或等于小数位。
文档正确,它们可以相同。但是,如果它们相同,则字段无法处理大于或等于
1.0
的值。
aaf_1kg_all = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True)
aaf_1kg_all = models.DecimalField(blank=True, null=True, max_digits=20,  decimal_places=10)