V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2022-08-14 22:57:48 +08:00 · 2072 次点击
    这是一个创建于 610 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

    11 条回复    2022-08-15 22:06:30 +08:00
    vone
        1
    vone  
       2022-08-14 23:30:24 +08:00
    order by case when expires<now() then 0 else 1 end
    kennir
        2
    kennir  
       2022-08-14 23:32:43 +08:00 via iPhone
    如果是我会考虑用
    chenjjl
        3
    chenjjl  
       2022-08-14 23:46:23 +08:00
    查询文章不分页吗?不分页的话这么做倒是没啥毛病
    zmaplex
        4
    zmaplex  
       2022-08-15 00:40:11 +08:00 via Android
    1L 已经给出了答案,SQL 取文章的时候过滤下置顶到期时间就可以了。
    ChoateYao
        5
    ChoateYao  
       2022-08-15 01:51:04 +08:00
    定时任务、延迟队列
    param
        6
    param  
       2022-08-15 07:59:10 +08:00 via Android
    django 的话,用 annotate 算出时间差吧
    param
        7
    param  
       2022-08-15 08:01:07 +08:00 via Android
    objects.annotate(F("top_at")-timezone.now())
    DreamSpace
        8
    DreamSpace  
       2022-08-15 09:22:04 +08:00
    置顶列表存 redis ,再设个过期时间。
    yinft
        9
    yinft  
       2022-08-15 13:44:55 +08:00
    一楼的 sql 就够用了
    python30
        10
    python30  
    OP
       2022-08-15 22:06:07 +08:00
    @vone
    @zmaplex
    @yinft
    我是用的 django 开发的。
    要用原生的 sql 语句? 还是怎么
    python30
        11
    python30  
    OP
       2022-08-15 22:06:30 +08:00
    @param 谢谢。在研究
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5567 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 08:29 · PVG 16:29 · LAX 01:29 · JFK 04:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.