sqlalchemy實(shí)現(xiàn)時間列自動更新教程
一、使用場景需求
1、在實(shí)際項(xiàng)目開發(fā)過程中,用戶可以操作的數(shù)據(jù),我們往往會新增一個字段,來保存用戶最后一次修改時間
2、一些系統(tǒng)中,我們需要存儲用戶最后一次登錄時間,來統(tǒng)計(jì)用戶的活躍度
二、 在sqlalchemy中常規(guī)的做法
1、數(shù)據(jù)模型
import datetime from uuid import uuid4 from sqlalchemy import Column, Integer, String, DateTime, Boolean from sqlalchemy_demo.connect import Base class UserModule(Base): """ 創(chuàng)建一個用戶的數(shù)據(jù)模型 """ __tablename__ = 'user' uuid = Column(String(36), unique=True, nullable=False, default=lambda: str(uuid4()), comment='uuid') id = Column(Integer, primary_key=True, autoincrement=True, comment='用戶id') user_name = Column(String(30), nullable=False, unique=True, comment='用戶名') password = Column(String(64), nullable=False, comment='用戶密碼') createtime = Column(DateTime, default=datetime.datetime.now, comment='創(chuàng)建時間') updatetime = Column(DateTime, default=datetime.datetime.now, comment='修改時間') is_lock = Column(Boolean, default=False, nullable=False, comment='是否鎖住用戶')
2、每次更新數(shù)據(jù)的時候,需要手動插入時間字段,來確保updatetime這個時間字段才會更新
三、使用自動更新數(shù)據(jù)
基于上面手動插入時間字段,在開發(fā)過程中很不方便,我們需要的是類似django中修改數(shù)據(jù),該列會自動更新
1、導(dǎo)包
from uuid import uuid4 from sqlalchemy import Column, Integer, String, DateTime, Boolean, TIMESTAMP, func from sqlalchemy.orm import relationship from sqlalchemy_demo.connect import Base
2、定義數(shù)據(jù)模型
class UserModule(Base): """ 創(chuàng)建一個用戶的數(shù)據(jù)模型 """ __tablename__ = 'user' uuid = Column(String(36), unique=True, nullable=False, default=lambda: str(uuid4()), comment='uuid') id = Column(Integer, primary_key=True, autoincrement=True, comment='用戶id') user_name = Column(String(30), nullable=False, unique=True, comment='用戶名') password = Column(String(64), nullable=False, comment='用戶密碼') createtime = Column(DateTime, server_default=func.now(), comment='創(chuàng)建時間') # onupdate設(shè)置自動更改 updatetime = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='修改時間') is_lock = Column(Boolean, default=False, nullable=False, comment='是否鎖住用戶')
3、接下來的創(chuàng)建表與增刪改查都一樣的。
補(bǔ)充知識:Flask-SQLALchemy對表中數(shù)據(jù)按時間進(jìn)行統(tǒng)計(jì)
例如表的結(jié)構(gòu)如下:
class Status(db.Model): id = db.Column(db.Integer, primary_key=True) submit_time = db.Column(db.DateTime, default=datetime.now())
其中,Status表接受用戶的提交,現(xiàn)在想對用戶的提交情況按時間進(jìn)行統(tǒng)計(jì)。例如過去24小時,每小時的提交次數(shù);過去12個月,每個月的提交次數(shù)。
python代碼實(shí)現(xiàn)查詢?nèi)缦拢?/p>
from datetime import datetime, timedelta NOW = datetime.utcnow() last_24h_submits_count = [] for h in xrange(1,25): count = session.query(Status).filter(Status.submit_time.between(NOW - timedelta(seconds=h*3600-1), NOW - timedelta(hours=h-1))).count() last_24h_submits_count.append(count)
以上這篇sqlalchemy實(shí)現(xiàn)時間列自動更新教程就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python爬蟲實(shí)現(xiàn)Cookie模擬登錄
這篇文章主要介紹了Python爬蟲實(shí)現(xiàn)Cookie模擬登錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Python中os.path.join函數(shù)的用法示例詳解
這篇文章主要給大家介紹了關(guān)于Python中os.path.join函數(shù)用法的相關(guān)資料,os.path.join函數(shù)是Python標(biāo)準(zhǔn)庫中的一個函數(shù),用于將多個路徑組合成一個有效的路徑,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10Python實(shí)現(xiàn)在tkinter中使用matplotlib繪制圖形的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)在tkinter中使用matplotlib繪制圖形的方法,結(jié)合實(shí)例形式分析了Python使用tkinter與matplotlib進(jìn)行正弦曲線圖形繪制的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Python3.7安裝keras和TensorFlow的教程圖解
這篇文章主要介紹了Python3.7安裝keras和TensorFlow經(jīng)驗(yàn),本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10python目標(biāo)檢測實(shí)現(xiàn)黑花屏分類任務(wù)示例
這篇文章主要為大家介紹了python目標(biāo)檢測實(shí)現(xiàn)黑花屏分類任務(wù)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07