关于Django 模型字段 `choices`自定义数据类型的枚举——补充

文章目录

    • [1. 处理 `datetime` 类型的 `choices`](#1. 处理 datetime 类型的 choices)
    • [2. 处理 `time` 类型的 `choices`](#2. 处理 time 类型的 choices)
    • [3. 处理 `Decimal` 类型的 `choices`](#3. 处理 Decimal 类型的 choices)
    • [4. 处理 `UUID` 类型的 `choices`](#4. 处理 UUID 类型的 choices)
    • [5. 处理 `float` 类型的 `choices`](#5. 处理 float 类型的 choices)

Choices 类的基础上扩展,可以将 choices 与特定数据类型(如 datedatetime)结合,适合需要特定类型支持的场景,如日期 date

python 复制代码
import datetime

class MoonLandings(datetime.date, models.Choices):
    APOLLO_11 = 1969, 7, 20, "Apollo 11 (Eagle)"
    APOLLO_12 = 1969, 11, 19, "Apollo 12 (Intrepid)"

类似的方式还可以应用于处理其他特定数据类型,例如 datetimetimeDecimalUUID 等。以下是一些常见数据类型的示例:

1. 处理 datetime 类型的 choices

假设我们需要记录一些重要的时间点,可以使用 datetime.datetimemodels.Choices 来定义这些选择:

python 复制代码
import datetime
from django.db import models

class ImportantMoments(datetime.datetime, models.Choices):
    NEW_YEAR = 2024, 1, 1, 0, 0, 0, "New Year 2024"
    SUMMER_SOLSTICE = 2024, 6, 21, 12, 0, 0, "Summer Solstice 2024"
    WINTER_SOLSTICE = 2024, 12, 21, 12, 0, 0, "Winter Solstice 2024"

class Event(models.Model):
    moment = models.DateTimeField(choices=ImportantMoments.choices)

这里的 moment 字段的可选项是特定的 datetime 值,比如"2024 年的新年"等。这样我们可以确保用户只能选择预定义的时间点。

2. 处理 time 类型的 choices

如果需要记录某些特定的时间(比如营业时间、特殊的时刻),可以用 datetime.timemodels.Choices

python 复制代码
import datetime
from django.db import models

class BusinessHours(datetime.time, models.Choices):
    OPENING = 9, 0, 0, "Opening Time"
    CLOSING = 18, 0, 0, "Closing Time"
    LUNCH_BREAK = 12, 0, 0, "Lunch Break"

class Store(models.Model):
    operation_time = models.TimeField(choices=BusinessHours.choices)

在这个例子中,operation_time 字段的可选值只能是 BusinessHours 定义的几个特定时刻,比如开门时间、关门时间和午休时间。

3. 处理 Decimal 类型的 choices

有时我们需要记录固定的金额,可以用 Decimalmodels.Choices。例如,对于定价的选择,可以使用 Decimal 来精确表示金额。

python 复制代码
from decimal import Decimal
from django.db import models

class FixedPrices(Decimal, models.Choices):
    BUDGET = Decimal("9.99"), "Budget Option"
    STANDARD = Decimal("19.99"), "Standard Option"
    PREMIUM = Decimal("29.99"), "Premium Option"

class Product(models.Model):
    price = models.DecimalField(max_digits=5, decimal_places=2, choices=FixedPrices.choices)

在这里,price 字段的选项是特定的价格,比如"预算选项"、"标准选项"等,可以确保用户只能选择固定的金额。

4. 处理 UUID 类型的 choices

如果使用 UUID 来标识某些特定的设备或版本,我们可以用 uuid.UUIDmodels.Choices。例如,在记录设备时,指定唯一的 UUID 作为选项:

python 复制代码
import uuid
from django.db import models

class KnownDevices(uuid.UUID, models.Choices):
    DEVICE_A = uuid.UUID("123e4567-e89b-12d3-a456-426614174000"), "Device A"
    DEVICE_B = uuid.UUID("123e4567-e89b-12d3-a456-426614174001"), "Device B"
    DEVICE_C = uuid.UUID("123e4567-e89b-12d3-a456-426614174002"), "Device C"

class Device(models.Model):
    device_id = models.UUIDField(choices=KnownDevices.choices)

这样可以确保 device_id 字段只能是已知设备的 UUID

5. 处理 float 类型的 choices

在某些情况下,如果我们需要用浮点数记录物理常量(比如π值的不同精度),可以使用 float 类型。

python 复制代码
from django.db import models

class PiValues(float, models.Choices):
    LOW_PRECISION = 3.14, "Low Precision Pi"
    MEDIUM_PRECISION = 3.14159, "Medium Precision Pi"
    HIGH_PRECISION = 3.1415926535, "High Precision Pi"

class Measurement(models.Model):
    pi_value = models.FloatField(choices=PiValues.choices)

通过这种方法,pi_value 字段仅限于选择特定精度的 π 值。

相关推荐
A小辣椒13 小时前
Vue + Django 项目部署指南
django
A小辣椒13 小时前
Django 项目数据库迁移 + 旧数据导入
django
大叔_爱编程16 小时前
基于大数据的短视频用户兴趣分析-hive+django+spider
大数据·hive·django·毕业设计·源码·课程设计·spider
DreamNotOver19 小时前
使用 Django 测试脚本验证用户角色与权限:自动化测试用户仪表盘访
数据库·mysql·django·sqlite
U盘失踪了2 天前
Django Bootstrap 下载到本地使用,避免 CDN 超时
python·django·bootstrap
U盘失踪了2 天前
Django 登录注册功能实现
后端·python·django
U盘失踪了2 天前
Django 登录注册功能实现-样式优化
后端·python·django
B站计算机毕业设计之家2 天前
大数据项目:基于python电商平台用户行为数据分析可视化系统 电商订单数据分析 Django框架 Echarts可视化 大数据技术(建议收藏)
大数据·python·机器学习·数据分析·django·电商·用户分析
凡梦千华3 天前
django迁移表结构和数据
数据库·django
猎人everest3 天前
Django Rest Framework (DRF) 核心知识体系梳理与深度讲解
后端·python·django