Python:peewee工具函数model_to_dict的用法参数详解

简介: Python:peewee工具函数model_to_dict的用法参数详解

定义

model_to_dict(
  model[, 
    recurse=True[, 
      backrefs=False[, 
        only=None[, 
          exclude=None[, 
            extra_attrs=None[, 
              fields_from_query=None[, 
                max_depth=None[, 
                  manytomany=False
]]]]]]]])

参数

recurse (bool) – Whether foreign-keys should be recursed.
backrefs (bool) – Whether lists of related objects should be recursed.
only – A list (or set) of field instances which should be included in the result dictionary.
exclude – A list (or set) of field instances which should be excluded from the result dictionary.
extra_attrs – A list of attribute or method names on the instance which should be included in the dictionary.
fields_from_query (Select) – The SelectQuery that created this model instance. Only the fields and values explicitly selected by the query will be serialized.
max_depth (int) – Maximum depth when recursing.
manytomany (bool) – Process many-to-many fields.

示例

模型定义

# -*- coding: utf-8 -*-
import json
from datetime import datetime
from peewee import CharField, IntegerField, DateTimeField, BooleanField, TextField
class UserModel(BaseModel):
    """用户表"""
    id = IntegerField(primary_key=True)
    # 域名
    name = CharField()
    # 密码
    password= CharField(default="")
    # 分组
    age = IntegerField(default=0)
    # 创建时间
    create_time = DateTimeField(default=datetime.now)
    # 更新时间
    update_time = DateTimeField(default=datetime.now)
  # 计算属性
    @property
    def username(self):
       return '用户' + self.id
user = UserModel.get_by_id(1)
model_to_dict(
        model=user,
        # 排除密码字段
        exclude=[DomainModel.password],
        # 增加计算属性字段
        extra_attrs=[
            'username'
        ]
    )

参考

https://docs.peewee-orm.com/en/latest/peewee/playhouse.html#model_to_dict

https://github.com/coleifer/peewee/issues/2386


相关文章
|
5天前
|
设计模式 缓存 监控
Python装饰器:优雅增强函数功能
Python装饰器:优雅增强函数功能
181 101
|
12天前
|
缓存 测试技术 Python
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
162 99
|
12天前
|
存储 缓存 测试技术
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
143 98
|
16天前
|
缓存 Python
Python中的装饰器:优雅地增强函数功能
Python中的装饰器:优雅地增强函数功能
|
26天前
|
存储 缓存 测试技术
理解Python装饰器:简化代码的强大工具
理解Python装饰器:简化代码的强大工具
|
14天前
|
机器学习/深度学习 编解码 Python
Python图片上采样工具 - RealESRGANer
Real-ESRGAN基于深度学习实现图像超分辨率放大,有效改善传统PIL缩放的模糊问题。支持多种模型版本,推荐使用魔搭社区提供的预训练模型,适用于将小图高质量放大至大图,放大倍率越低效果越佳。
|
8天前
|
JSON 缓存 开发者
淘宝商品详情接口(item_get)企业级全解析:参数配置、签名机制与 Python 代码实战
本文详解淘宝开放平台taobao.item_get接口对接全流程,涵盖参数配置、MD5签名生成、Python企业级代码实现及高频问题排查,提供可落地的实战方案,助你高效稳定获取商品数据。
|
2月前
|
Python
Python 函数定义
Python 函数定义
110 1
|
24天前
|
算法 安全 数据安全/隐私保护
Python随机数函数全解析:5个核心工具的实战指南
Python的random模块不仅包含基础的随机数生成函数,还提供了如randint()、choice()、shuffle()和sample()等实用工具,适用于游戏开发、密码学、统计模拟等多个领域。本文深入解析这些函数的用法、底层原理及最佳实践,帮助开发者高效利用随机数,提升代码质量与安全性。
123 0
一分钟看懂Python中的 // 和 / 和 % 的用法区别
一分钟看懂Python中的 // 和 / 和 % 的用法区别

推荐镜像

更多