• 介绍 Django 的模型字段 DecimalField


        DecimalField 是一种模型字段,相当于数据库字段的属性 decimal(x,y)。在 Python 中用一个 Decimal 实例来表示,表现为一个固定精度的十进制数。它使用 DecimalValidator 验证输入。
        语法:

    field_name = models.DecimalField(max_digits=None, decimal_places=None, **options)
    
    • 1

        DecimalField 具有以下必需参数:

    • DecimalField.max_digits
      数字中允许的最大位数。请注意,此数字必须大于或等于 decimal_places。
    • DecimalField.decimal_places
      与数字一起存储的小数位数。

        例如,要存储最高为 999.99 的数字,精度为小数点后 2 位,您可以使用:

    models.DecimalField(..., max_digits=5, decimal_places=2)
    
    • 1

        以10位小数的精度存储大约10亿的数字:

    models.DecimalField(..., max_digits=19, decimal_places=10)
    
    • 1
    DecimalField 示例

        使用示例说明 DecimalField。假设一个名为 testproject 的项目,该项目有一个名叫 testapp 的应用。在 testapp 应用的 models.py 文件中输入以下代码:

    from django.db import models
    
    # Create your models here.
    
    class TestModel(models.Model):
        test_field = models.DecimalField(max_digits=5, decimal_places=2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

        在 settings.py 文件的 INSTALLED_APPS 选项中增加 testapp:

    # Application definition
    
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'testapp',
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

        现在在终端执行 makemigrations 命令:
    在这里插入图片描述
        testapp 文件夹中将会出现一个名为 migrations 的文件夹,migrations 文件夹中有一个名为 0001_initial.py 的文件。
    在这里插入图片描述
        执行:

    python manage.py migrate
    
    • 1

        最终会在表 testapp_testmodel 中,创建一个名为 test_field 的 DecimalField 字段。表名组成结构为:应用名_类名(如:testapp_testmodel)。
    在这里插入图片描述
        注意:尽管我们没有在 models 给表设置主键,但是 Django 会自动添加一个 id 作为主键。

    DecimalField 用途

        DecimalField 用于存储数据库中的 python datetime.date 实例,也可以在数据库中存储任何类型的十进制数。让我们尝试在上面创建的模型中存储一个十进制数。
        修改 testapp 下的 admin.py :

    from django.contrib import admin
    
    # Register your models here.
    
    # importing the model from testapp app
    from testapp.models import TestModel
    
    # importing datetime module
    import decimal
    
    # creating an instance of datetime.date
    d = decimal.Decimal(9.53)
    
    # creating an instance of TestModel
    test_object = TestModel.objects.create(test_field = d)
    test_object.save()
    
    admin.site.register(TestModel)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

        在管理后台中检查,我们已经创建了一个 TestModel 实例。
    在这里插入图片描述
        内容为:
    在这里插入图片描述

    参考文档

    [1].NaveenArora. DecimalField – Django Models[EB/OL]. [2022-10-27]. https://www.geeksforgeeks.org/decimalfield-django-models/.

  • 相关阅读:
    韩语难学吗
    DeFi 入门必备:你需要了解的 DeFi 重要词语
    SpringBoot+vue+elementui实现前后端分离的化妆品销售商城网站
    深度学习应用篇-计算机视觉-图像分类[3]:ResNeXt、Res2Net、Swin Transformer、Vision Transformer等模型结构、实现、模型特点详细介绍
    【Microelectronic Systems】
    std::packaged_task 源码分析
    Linux tmux使用总结
    C++学习笔记(Ⅲ):C++核心编程
    GENERALIZATION THROUGH MEMORIZATION: NEAREST NEIGHBOR LANGUAGE MODELS
    为什么不建议使用Python自带的logging?
  • 原文地址:https://blog.csdn.net/zsx0728/article/details/127548287