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


相关文章
|
2月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
175 1
|
2月前
|
算法 Java Docker
(Python基础)新时代语言!一起学习Python吧!(三):IF条件判断和match匹配;Python中的循环:for...in、while循环;循环操作关键字;Python函数使用方法
IF 条件判断 使用if语句,对条件进行判断 true则执行代码块缩进语句 false则不执行代码块缩进语句,如果有else 或 elif 则进入相应的规则中执行
270 1
|
2月前
|
Java 数据处理 索引
(numpy)Python做数据处理必备框架!(二):ndarray切片的使用与运算;常见的ndarray函数:平方根、正余弦、自然对数、指数、幂等运算;统计函数:方差、均值、极差;比较函数...
ndarray切片 索引从0开始 索引/切片类型 描述/用法 基本索引 通过整数索引直接访问元素。 行/列切片 使用冒号:切片语法选择行或列的子集 连续切片 从起始索引到结束索引按步长切片 使用slice函数 通过slice(start,stop,strp)定义切片规则 布尔索引 通过布尔条件筛选满足条件的元素。支持逻辑运算符 &、|。
163 0
|
3月前
|
设计模式 缓存 监控
Python装饰器:优雅增强函数功能
Python装饰器:优雅增强函数功能
273 101
|
3月前
|
缓存 测试技术 Python
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
215 99
|
3月前
|
存储 缓存 测试技术
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
191 98
|
3月前
|
缓存 Python
Python中的装饰器:优雅地增强函数功能
Python中的装饰器:优雅地增强函数功能
|
2月前
|
机器学习/深度学习 PyTorch 算法框架/工具
python torch基础用法
本教程系统讲解PyTorch基础,涵盖张量操作、自动求导、神经网络构建、训练流程、GPU加速及模型保存等核心内容,结合代码实例帮助初学者快速掌握深度学习开发基础,是入门PyTorch的实用指南。
474 6
|
3月前
|
机器学习/深度学习 编解码 Python
Python图片上采样工具 - RealESRGANer
Real-ESRGAN基于深度学习实现图像超分辨率放大,有效改善传统PIL缩放的模糊问题。支持多种模型版本,推荐使用魔搭社区提供的预训练模型,适用于将小图高质量放大至大图,放大倍率越低效果越佳。
264 3
|
3月前
|
JSON 缓存 开发者
淘宝商品详情接口(item_get)企业级全解析:参数配置、签名机制与 Python 代码实战
本文详解淘宝开放平台taobao.item_get接口对接全流程,涵盖参数配置、MD5签名生成、Python企业级代码实现及高频问题排查,提供可落地的实战方案,助你高效稳定获取商品数据。

推荐镜像

更多