1

我在模型中添加了一个 DecimalField

dec_field = models.DecimalField(max_digits=6, decimal_places=3)

在我尝试使用 South 创建一个自动架构迁移文件后,我尝试指定一个一次性值以用于现有列,但是......

? Please enter Python code for your one-off default value.
? The datetime module is available, so you can do e.g. datetime.date.today()
>>> Decimal('15.4')
! Invalid input: name 'Decimal' is not defined
>>> from decimal import *
! Invalid input: invalid syntax (<string>, line 1)

我是否可以指定 Decimal 默认值(无损,所以浮点数并不理想),如果可以,如何?

4

2 回答 2

3

查看代码:: https://bitbucket.org/andrewgodwin/south/src/81b93bfc927b46227103d094691b5ddcfc25f400/south/creator/actions.py?at=default#cl-190

我试过这样做:

    __import__('decimal').Decimal('0.445')

并工作

但这很奇怪,最正确的方法就是放一个它自动转换的字符串

或导入 Decimal,因为南迁移创造了

于 2013-02-22T18:19:20.717 回答
2

接受的答案确实很奇怪。

The correct answer is that South migrations are, by design, user-editable, and schemamigration --auto doesn't always do a perfect work. In this case, I would just enter a value that is easy to find in the file (say, '0.445'), then edit the file, add a proper import for Decimal and modify the code to use Decimal('0.445') instead of the value in the generated migration.

于 2014-08-22T11:02:00.753 回答