Tortoise-orm 使用(二) 元类及Mixin

发布时间:2024年01月17日

元类

一个简单示例

from enum import Enum

from tortoise import fields
from tortoise.models import Model


class Gender(Enum):
    MALE = "male"
    FEMALE = "female"


class UserModel(Model):
    id = fields.IntField(pk=True)
    username = fields.CharField(max_length=50, description="用户名")
    nickName = fields.CharField(max_length=50, description="昵称")
    gender = fields.CharEnumField(Gender, default=None, description="性别")
    avatar = fields.CharField(max_length=256, null=True, description="头像")
    address = fields.CharField(max_length=256, null=True, description="地址")
    email = fields.CharField(max_length=256, null=True, description="邮箱")
    password = fields.CharField(max_length=50, description="密码")
    createTime = fields.DatetimeField(auto_now_add=True, description="创建事件")
    updateTime = fields.DatetimeField(auto_now=True, description="更新时间")
    enable = fields.BooleanField(default=False, description="是否启用")

    class Meta:
        table = "user"

此段代码需要说明的是

createTime - auto_now_add 只在第一次保存时候添加

updateTime -?auto_now 每次保存都会更新

? ? class Meta:
? ? ? ? table = "user" # 指定表名称,元类应用一

抽象公共部分

from enum import Enum

from tortoise import fields
from tortoise.models import Model


class Gender(Enum):
    MALE = "male"
    FEMALE = "female"


class BaseModel(Model):
    id = fields.IntField(pk=True)

    class Meta:
        abstract = True


class UserModel(BaseModel):
    username = fields.CharField(max_length=50, description="用户名")
    nickName = fields.CharField(max_length=50, description="昵称")
    gender = fields.CharEnumField(Gender, default=None, description="性别")
    avatar = fields.CharField(max_length=256, null=True, description="头像")
    address = fields.CharField(max_length=256, null=True, description="地址")
    email = fields.CharField(max_length=256, null=True, description="邮箱")
    password = fields.CharField(max_length=50, description="密码")
    createTime = fields.DatetimeField(auto_now_add=True, null=True, description="创建事件")
    updateTime = fields.DatetimeField(auto_now=True, null=True, description="更新时间")
    enable = fields.BooleanField(default=False, description="是否启用")

    class Meta:
        table = "user"

代码说明

# 抽象出来id这个公共的部分,元类定义为抽象就不生成实际的表

class BaseModel(Model):
? ? id = fields.IntField(pk=True)

? ? class Meta:
? ? ? ? abstract = True # 定义抽象模型,元类应用二

# 在下面的类定义中直接继承BaseModel即可

class UserModel(BaseModel):
? ? ? ? ...

Mixin更进一步抽象

mixin介绍

Mixin 是一种多重继承的设计模式,它允许将多个类的功能组合在一起,以便在不创建新类的情况下扩展现有类的功能。
在 Python 中,Mixin 通常是定义在一个单独的模块中,并通过 import 语句将其导入到其他模块中使用。Mixin 类通常包含一些方法和属性,这些方法和属性可以被其他类继承和重用。

本地案例

from enum import Enum

from tortoise import fields
from tortoise.models import Model


class Gender(Enum):
    MALE = "male"
    FEMALE = "female"


class NameMixin:
    username = fields.CharField(50, unique=True, description="用户名")


class DateTimeMixin:
    createTime = fields.DatetimeField(auto_now_add=True, null=True, description="创建事件")
    updateTime = fields.DatetimeField(auto_now=True, null=True, description="更新时间")


class BaseModel(Model):
    id = fields.IntField(pk=True)

    class Meta:
        abstract = True


class UserModel(BaseModel, NameMixin, DateTimeMixin):
    nickName = fields.CharField(max_length=50, description="昵称")
    gender = fields.CharEnumField(Gender, default=None, description="性别")
    avatar = fields.CharField(max_length=256, null=True, description="头像")
    address = fields.CharField(max_length=256, null=True, description="地址")
    email = fields.CharField(max_length=256, null=True, description="邮箱")
    password = fields.CharField(max_length=50, description="密码")
    enable = fields.BooleanField(default=False, description="是否启用")

    class Meta:
        table = "user"


class RoleModel(BaseModel, NameMixin):
    code = fields.CharField(max_length=40, description="角色字符串")
    enable = fields.BooleanField(default=False, description="是否启用")

    class Meta:
        table = "roles"

执行命令后,生成的数据表结构

结束

文章来源:https://blog.csdn.net/wzbhbb/article/details/135643317
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。