推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
efrigate43
V2EX  ›  Python

请教如何在 sqlalchemy model 的 event 里面停止删除或者用其他方式实现软删除

  •  
  •   efrigate43 · Feb 15, 2022 · 3012 views
    This topic created in 1688 days ago, the information mentioned may be changed or developed.

    如题

    model

    
    def crudset(classModel):
    from datetime import datetime as dt
    
        @event.listens_for(classModel, "before_insert")
        def model_brfore_insert(target, value, initiator):
            initiator.dt_create = dt.now().strftime("%Y-%m-%d %H:%M:%S")
    
        @event.listens_for(classModel, "before_update")
        def model_brfore_update(target, value, initiator):
            initiator.dt_update = dt.now().strftime("%Y-%m-%d %H:%M:%S")
    
        @event.listens_for(classModel, "before_delete")
        def model_before_delete(target, value, initiator):
            from copy import deepcopy
    
            item = deepcopy(initiator)
            item.dt_delete = dt.now().strftime("%Y-%m-%d %H:%M:%S")
    
            print(str(item))
        
    

    正在写 model_before_delete 这个方法,如何能把 dt_delete 这个字段的值 作为软删除的标志,并且停止当前删除的操作,已达到软删的目的,又或者有什么软删的附加库可用吗?

    我初学者刚接触不久 请各位大佬赐教

    3 replies  •  2022-02-15 10:50:59 +08:00
    Latin
        1
    Latin  
       Feb 15, 2022
    手动实现 软删除标识字段赋值
    lolizeppelin
        3
    lolizeppelin  
       Feb 15, 2022
    如果你的删除是 object.delete()的方式 重写 delete 方式就好把?

    全局的话在监听事件里似乎不能直接做到啊,sqlalchemy 代码太复杂了建议不要这么搞....
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2513 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 11:10 · PVG 19:10 · LAX 04:10 · JFK 07:10
    ♥ Do have faith in what you're doing.