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

相关推荐
梅雅达编程笔记8 小时前
零基础学 Python 第14章 | 模块、包与第三方库
开发语言·python·django·numpy·pandas
梅雅达编程笔记10 小时前
零基础学 Python 第15章 | 类与对象:面向对象编程入门
开发语言·python·django·numpy·pandas
2401_868534782 天前
OSPF经典案例分析
python·django
不瘦80斤不改名2 天前
全家桶、乐高积木与类型引擎:重新认识 Django、Flask 与 FastAPI
django·flask·fastapi
hanxiuchao6 天前
告别客户端臃肿!网页端 M3U8 播放调试方案,适配全办公场景
运维·python·django·m3u8·m3u8播放
程序员羽痕6 天前
基于深度学习的眼疾识别系统
人工智能·pytorch·深度学习·分类·django
流云鹤7 天前
1. 配置环境、创建导航栏
python·django
流云鹤7 天前
2.登录模块
python·django
流云鹤7 天前
专题栏说明
python·django
XGeFei8 天前
【Django学习笔记】—— Django 高并发通俗讲解
笔记·学习·django