推荐学习书目
› 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
python30
V2EX  ›  Python

django 项目,文章到期后自动取消置顶,这个时间如何处理好?

  •  
  •   python30 · Aug 14, 2022 · 3493 views
    This topic created in 1545 days ago, the information mentioned may be changed or developed.

    比如一个用户置顶一个文章,置顶了 10 天。

    到期后这个文章自动取消置顶。

    我的想法是
    用时间差与这个置顶时间判断一下。
    如果大于这个时间,就在文章加裁的时候执行一个函数
    把置顶的字段设置为:is_top = False

    但是这样感觉哪里总有点不对的地方。
    大家对这个有什么思路吗?
    谢谢。

    11 replies  •  2022-08-15 22:06:30 +08:00
    vone
    vone
    Aug 14, 2022
    1
    order by case when expires<now() then 0 else 1 end
    kennir
    kennir
    Aug 14, 2022 via iPhone
    2
    如果是我会考虑用
    chenjjl
    chenjjl
    Aug 14, 2022
    3
    查询文章不分页吗?不分页的话这么做倒是没啥毛病
    huangzhiyia
    huangzhiyia
    Aug 15, 2022 via Android
    4
    1L 已经给出了答案,SQL 取文章的时候过滤下置顶到期时间就可以了。
    ChoateYao
    ChoateYao
    Aug 15, 2022
    5
    定时任务、延迟队列
    param
    param
    Aug 15, 2022 via Android
    6
    django 的话,用 annotate 算出时间差吧
    param
    param
    Aug 15, 2022 via Android
    7
    objects.annotate(F("top_at")-timezone.now())
    DreamSpace
    DreamSpace
    Aug 15, 2022
    8
    置顶列表存 redis ,再设个过期时间。
    yinft
    yinft
    Aug 15, 2022
    9
    一楼的 sql 就够用了
    python30
    python30
    OP
    Aug 15, 2022
    10
    @vone
    @zmaplex
    @yinft
    我是用的 django 开发的。
    要用原生的 sql 语句? 还是怎么
    python30
    python30
    OP
    Aug 15, 2022
    11
    @param 谢谢。在研究
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1662 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 00:59 · PVG 08:59 · LAX 16:59 · JFK 19:59
    ♥ Do have faith in what you're doing.