Why is Django DecimalField attribute `max_digits` non-optional?

 ̄綄美尐妖づ 提交于 2020-01-16 00:34:28

问题


CommandError: System check identified some issues:

ERRORS:
(fields.E132) DecimalFields must define a 'max_digits' attribute.

Is there a technical reason why max_digits is a required attribute for Django's model field DecimalField?

The docs say it used Python's [decimal][1] module, but python object type doesn't seem to be bothered at all by anything to do with the absolute number of digits of the decimal object.

Maybe there's an opaque ORM reason?

FloatFields (against which DecimalFields are compared) don't require you to predetermine the number of digits so why decimals?

I know this small additional attribute shouldn't bother me but for some reason it's seemed unnecessary to me ever since the first time I used this field type.


回答1:


It's required by the database (not the ORM). SQL decimal columns are declared with both precision (total number of significant digits) and scale (number of digits to the right of the decimal point).

See for instance the formal grammar quoted at this answer: https://stackoverflow.com/a/759606/2337736



来源:https://stackoverflow.com/questions/30960589/why-is-django-decimalfield-attribute-max-digits-non-optional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!