首页 > 解决方案 > 自动删除超过 30 天的博文

问题描述

我想在我的 Django 项目中自动删除超过 30 天的博客文章。我不知道该怎么做。

标签: djangodjango-templates

解决方案


这很简单。

创建一个管理命令,每月使用 crontab 调用它。

crontab - https://tecadmin.net/crontab-in-linux-with-20-examples-of-cron-schedule/

管理命令 Django - https://simpleisbetterthancomplex.com/tutorial/2018/08/27/how-to-create-custom-django-management-commands.html

from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = 'Delete blog post'

    def handle(self, *args, **kwargs):
        # Do stuff here - Delete posts

crontab

0 0 * * *  python /path/to/manage.py mycommand

推荐阅读