django - How to auto delete the expires data in the database? -


if store row data in database table(instance), , table has field names expire_time. if time on expire_time, want delete row data.

so, if want that, can every time query table, traverse every row data, if expires, delete.

but if don't query can not realize requirement.

so, if there method that?

i use python django, database mariadb.

you can write custom management command you. save in myapp/management/commands/delete_expired.py example:

from django.core.management.base import basecommand django.utils import timezone myapp.models import mymodel   class command(basecommand):     = 'deletes expired rows'      def handle(self, *args, **options):         = timezone.now()         mymodel.objects.filter(expire_time__lt=now).delete() 

then either call command cron task or queue. on command line can call:

python manage.py delete_expired


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -