+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2020-04(10)

2020-05(22)

2020-06(45)

2020-07(31)

2020-08(32)

sqlalchemy格式化datetime类型json输出数据

发布于2021-04-03 20:29     阅读(2036)     评论(0)     点赞(19)     收藏(5)


0

1

2

3

4

5

6

7

1、模型

from sqlalchemy import Column, Integer, Sequence, String, func, DateTime
from global_conf.settings import Base


class Users(Base):
    __tablename__ = 'users'
    id = Column(Integer, Sequence('id_seq'), primary_key=True, autoincrement=True)
    username = Column(String(100), unique=True)
    password = Column(String(100))
    remarks = Column(String(100))
    create_time = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='创建时间')

    def __init__(self, username, password, remarks, create_time):
        self.username = username
        self.password = password
        self.remarks = remarks
        self.create_time = create_time


    def __repr__(self):
        return "<Users('%s')>" % self.username



2、view.py

# coding=utf-8
from bottle import route, jinja2_template
from apps.model import Users
from global_conf.settings import db
from sqlalchemy import func


@route('/', method=['GET', 'POST'])
def index():
    users = db.query(Users.username, Users.password, Users.remarks, func.date_format(Users.create_time, "%Y-%m-%d %H:%i:%s")).all()
    return {'u': list(users)}

时间格式化:

func.date_format(Users.create_time, "%Y-%m-%d %H:%i:%s")

时间戳格式化:

func.from_unixtime((Users.create_time), "%Y-%m-%d %H:%i:%s")

原文链接:https://blog.csdn.net/human_soul/article/details/115396697

0

1

2

3

4

5

6

7

8

9



所属网站分类: 技术文章 > 博客

作者:高富帅

链接: https://www.pythonheidong.com/blog/article/915682/fede36f1c9dacbd1977a/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

19 0
收藏该文
已收藏

评论内容:(最多支持255个字符)