关于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 字段仅限于选择特定精度的 π 值。

相关推荐
Muyuan199825 分钟前
28.Paper RAG Agent 开发记录:修复 LLM Rerank 的解析、Fallback 与可验证性
linux·人工智能·windows·python·django·fastapi
Muyuan19981 天前
27.RAG 系统中的上下文充分性判断:从 Chunk 数量、FAISS 距离到 LLM Relevance Gate
python·django·pdf·fastapi·faiss
程序媛徐师姐1 天前
Python基于Django的小区果蔬预定系统【附源码、文档说明】
python·django·小区果蔬预定系统·果蔬预定·python小区果蔬预定系统·小区果蔬预定·python果蔬预定系统
码界筑梦坊2 天前
111-基于Python的中国旅游用户数据可视化分析系统
python·信息可视化·django·毕业设计·旅游
YJlio2 天前
10.2.8 以其他账户运行服务(Running services in alternate accounts):为什么“把服务切到某个用户账号下运行”,本质上是在改变服务的整个安全上下文?
python·安全·ios·机器人·django·iphone·7-zip
小熊Coding2 天前
懂车帝汽车销售数据可视化分析系统
python·信息可视化·django·汽车·数据可视化分析·懂车帝·汽车销售数据分析
ma_de_hao_mei_le3 天前
ntquerysystemiunfomation 数据传递
django
Muyuan19983 天前
22.让 RAG Agent 更像真实产品:聊天页面优化、PDF 上传、知识库重建与检索片段展示
python·django·pdf·fastapi
Muyuan19983 天前
25.Paper RAG Agent 优化记录:上传反馈、计算器安全与 Chunk 参数调整
python·安全·django·sqlite·fastapi
Muyuan19983 天前
26.Paper RAG Agent 展示面收口:截图与项目表达更新记录
人工智能·python·django·fastapi