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