如何从 Django 模型创建双长文本字段

How to create double and long text field from django models

如何使用 Django 模型在 mysql 中创建 double 字段。我 运行 进入此错误以及如何使用 django

创建 longtext 数据类型
class Test(models.Model):
   maxcount = models.DoubleField(null=True) ## double DEFAULT NULL,
   cs = models.TextField()#longtext,

错误:

    maxcount = models.DoubleField(null=True) ##double DEFAULT NULL,
    AttributeError: 'module' object has no attribute 'DoubleField'

Django 中没有 DoubleFieldFloatField [FloatField description in Django's Model field reference] 在 mysql.

中创建了一个 double 数据类型

Django 的 TextField 在 mysql 中创建了一个 LONGTEXT 类型。没有简单的方法来改变 TextField 行为来创建例如MEDIUMTEXT 输入 mysql。但是可以创建一个自定义模型字段来实现这一点:Writing custom model fields

https://docs.djangoproject.com/en/1.8/ref/models/fields/#bigintegerfield
https://docs.djangoproject.com/en/1.8/ref/models/fields/#decimalfield
https://docs.djangoproject.com/en/dev/ref/models/fields/#floatfield

在需要整数和小数字段的情况下使用 big int。
对 Char 字段使用 https://docs.djangoproject.com/en/1.8/ref/models/fields/#charfield 并设置最大和最小参数

我认为您可能需要 DecimalField:https://docs.djangoproject.com/en/2.0/ref/models/fields/#decimalfield。它适用于 MySQL,您也可以将其用于整数值。

声明模型中的字段

balance = models.DecimalField(max_digits=20, decimal_places=2, default=Decimal(0.00))

此外,考虑定义一个小数上下文来创建您的实际值,如下所示:

# defined in the settings.py file.
DECIMAL_CONTEXT = Context(prec=6, rounding=ROUND_DOWN)

这将确保通过您的应用程序兼容十进制对象,并创建如下值:

DECIMAL_CONTEXT.create_decimal(0)
DECIMAL_CONTEXT.create_decimal(500)
DECIMAL_CONTEXT.create_decimal(5)
DECIMAL_CONTEXT.create_decimal(0.50)